使用 Jest.js 模拟复杂模块

Mocking complex module using Jest.js

这是我想要模拟的模块的样子:

class TheModule {
  constructor() {
    this.subClass = new SubClass();
  }
}

class SubClass {
  constructor() {
    this.someMethod = () => 'Did Something great'; 
  }
}

module.exports = TheModule;

这是我要测试的 TheModule 用法:

const TheModule = require('./TheModule');

const method = () => {
  const theModule = new TheModule();
  return theModule.subClass.someMethod();
}

module.exports = method;

这是我的测试:

describe('method test', () => {
  
  it('should return "Test pass"', () => {
    jest.doMock('./TheModule');
    const theModuleMock = require('./TheModule');
    const method = require('./TheModuleUsage');
    const mock = () => 'Test pass';
    theModuleMock.subClass.someMethod.mockImplementation(() => mock);
    expect(method()).toEqual('Test pass');
  });
  
});

当我运行这个测试我得到TypeError: Cannot read property 'someMethod' of undefined

是否可以在不更改 TheModule 实现的情况下模拟此模块?

如果你要导出 SubClass 你可以模拟它而不改变 TheModule 但在你的情况下你应该模拟 SubClass 属性 in TheModule 显式地使用工厂例如:

describe('method test', () => {

  it('should return "Test pass"', () => {
    let mockedSomeMethod = jest.fn().mockImplementation(() => 'Test pass');
    jest.doMock('./TheModule', () => {
      // mock constructor
      return jest.fn().mockImplementation(() => {
        return { subClass: { someMethod: mockedSomeMethod  } }
      });
    });
    const method = require('./TheModuleUsage');
    expect(method()).toEqual('Test pass');
  });
});