我有一个使用第二个模块的模块。它们都是自定义模块,而不是NPM包。
我想确保moduleUnderTest
从foo
调用特定方法。
所以我正在使用jest.mock
并且我传递了一个与foo
具有相同签名的函数,但包含了jest spy函数而不是实际的实现。
我的印象是,当jest.mock
需要mockFoo
时,将foo
与moduleUnderTest
对象一起使用会注入模拟foo
而不是实际模块。
如果我在测试运行时检查foo
内的moduleUnderTest
是什么,我可以看到它确实是我的嘲笑foo。
但是当我到达expect
时,mockedFoo.met1
变得未定义。
为什么?
// ../foo/foo.js
const foo = arg => {
console.log(arg)
return {
met1: () => {},
met2: () => {},
}
}
module.exports = foo
// ..foo/index.js
// I am doing it this way so I can put the actual implementation in
// several different files, but can require the whole module with
// require('../foo') rather than require('../foo/foo')
module.exports = require('./foo')
// ./moduleUnderTest.js
const foo = require('../foo')('hi')
require('util').inspect(foo) // it seems that foo is indeed the mocked
// version here[0]
const moduleUnderTest = () => {
foo.met1()
}
module.exports = moduleUnderTest
// ./moduleUnderTest.test.js
const moduleUnderTest = require('./moduleUnderTest')
const mockFoo = () => ({
met1: jest.fn(),
met2: jest.fn(),
})
jest.mock('../foo')
test('foo.met1 is called', () => {
moduleUnderTest()
expect(mockFoo.met1).toHaveBeenCalledTimes(1) // NOPE![1]
})
// [0]
// { met1:
// { [Function: mockConstructor]
// _isMockFunction: true,
// getMockImplementation: [Function],
// mock: [Getter/Setter],
// mockClear: [Function],
// mockReset: [Function],
// mockReturnValueOnce: [Function],
// mockResolvedValueOnce: [Function],
// mockRejectedValueOnce: [Function],
// mockReturnValue: [Function],
// mockResolvedValue: [Function],
// mockRejectedValue: [Function],
// mockImplementationOnce: [Function],
// mockImplementation: [Function],
// mockReturnThis: [Function],
// mockName: [Function],
// getMockName: [Function],
// mockRestore: [Function] },
// met2:
// { [Function: mockConstructor]
// _isMockFunction: true,
// getMockImplementation: [Function],
// mock: [Getter/Setter],
// mockClear: [Function],
// mockReset: [Function],
// mockReturnValueOnce: [Function],
// mockResolvedValueOnce: [Function],
// mockRejectedValueOnce: [Function],
// mockReturnValue: [Function],
// mockResolvedValue: [Function],
// mockRejectedValue: [Function],
// mockImplementationOnce: [Function],
// mockImplementation: [Function],
// mockReturnThis: [Function],
// mockName: [Function],
// getMockName: [Function],
// mockRestore: [Function] } }
// [1]
// expect(jest.fn())[.not].toHaveBeenCalledTimes()
//
// jest.fn() value must be a mock function or spy.
// Received: undefined
答案 0 :(得分:2)
我发现了问题所在。
因为foo是通过执行函数来实例化的,所以调用mockFoo.met1会返回undefined,因为mockFoo是一个函数。对于要定义的mock1,我必须调用mockFoo()。met1。但是那时我得到了一个新的mockFoo实例,它与moduleUnderTest加载和调用的实例不同。
要解决这个问题,我必须以这种方式声明并设置模拟:
const mockMet1 = jest.fn()
const mockMet2 = jest.fn()
const mockFoo = () => ({
met1: mockMet1,
met2: mockMet2,
})
const moduleUnderTest = require('./moduleUnderTest')
jest.mock('../foo', mockFoo)
// ...snip...
afterEach(() => { // resets the called count etc
mockMet1.resetMock()
mockMet2.resetMock()
})
// ...snip...
expect(mockMet1).toHaveBeenCalledTimes(1)
// ...snip...