10

请教一下,使用 jest 的写测试的时候,怎么 mock 掉一个 module 里面的 generator 函...

 2 years ago
source link: https://www.v2ex.com/t/806736
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  JavaScript

请教一下,使用 jest 的写测试的时候,怎么 mock 掉一个 module 里面的 generator 函数?

  yazoox · 1 天前 · 357 次点击
// module1.js
export function a() {
  yield 1;
  yield 2;
  // blabla...
}

// module2.js
export function b() {
  dispatch(createActionForA()); // redux 发送一个消息,redux-saga 会把这个消息转到对应的 saga 函数,比如 a()。
}
// test.js
jest.mock("../../saga/moduel1", () => {
  const actual = jest.requireActual("../../saga/module1");
  return {
    ...actual,
    a: jest.fn() // 这样写是不能工作的
    })
  };
});

但,现在,我在测试 b 的时候,我想把 a mock 掉。上面这个方法,我用来 mock 一般的 module,都能够正常工作。但 generator 不行。

请教一下大神,有没有好的方法?

另,我也想过,监听或者 mock redux 里面的 action 和 action 方法貌似也不方便。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK