node: 8.9.4
os: macOS El Capitan (10.11.6)
When mocking a module that contains generator methods they are ignored and not properly mocked. I have created a slim example below.
module.js
function* generatorMethod() {
yield 1;
yield 2;
yield 3;
}
module.exports.generatorMethod = generatorMethod;
module.exports.asyncMethod = async () => {
return 1;
}
module.exports.syncMethod = () => {
return 1;
}
module.test.js
jest.mock('./module');
const mod = require('./module');
describe('generator module method', () =>{
test('does not get mocked', () => {
expect(mod.syncMethod).toBeDefined();
expect(mod.asyncMethod).toBeDefined();
expect(mod.generatorMethod).toBeDefined();
});
});
package.json
{
"name": "test",
"version": "1.0.0",
"scripts": {
"test": "jest"
},
"author": "",
"license": "MIT",
"description": "",
"dependencies": {
"jest": "^22.4.3"
}
}
The test fails because the generatorMethod is not defined on the mocked module.
Seems like, for some reason, this doesn't pick up generator functions: https://github.com/facebook/jest/blob/ef4862bee912901301836df48f6add8453179d96/packages/jest-mock/src/index.js#L650-L671
Help appreciated!
@SimenB Thanks for the pointer. I was able to dig around that code and found that it was pretty trivial to get generators properly mocked.
Hi @steven10172 can you share how to solve this problem?
Most helpful comment
Hi @steven10172 can you share how to solve this problem?