spr*_*ded 5 javascript mocking jestjs
我有一个使用第二个模块的模块。它们都是自定义模块,而不是 NPM 包。
我想确保moduleUnderTest从 调用特定方法foo。
因此,我正在使用jest.mock并向它传递一个具有相同签名但foo包含笑话间谍函数而不是实际实现的函数。
我的印象是,jest.mock与对象一起使用会在需要时mockFoo注入模拟模块foo而不是实际模块。moduleUnderTestfoo
如果我在测试运行时检查foo内部内容,我可以看到它确实是我模拟的 foo。moduleUnderTest
但是当我到达 时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
Run Code Online (Sandbox Code Playgroud)
我发现问题是什么了。
因为 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...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15935 次 |
| 最近记录: |