Skip to content

jest.mock does not mock generator methods in module #5979

Closed
@steven10172

Description

@steven10172

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions