开玩笑:无法使用 `jest.fn()` 保留模拟函数的引用
Jest: having trouble keeping a reference of a mock function with `jest.fn()`
我想模拟一个函数并确保它已经执行了一定次数。
棘手的部分是我想要模拟的函数是另一个函数返回结果的一部分
我的实现大致是这样的
const theFnIWantedToMock = jest.fn()
jest.mock('../hooks', () => {
const actualHooks = jest.requireActual('../hooks')
return {
...actualHooks,
someHooks() {
return
{
theFnIWantedToMock,
}
}
}
})
describe('test', () => {
it('some test', () => {
//...
expect(theFnIWantedToMock).toHaveBeenCalledTimes(1)
})
})
但是 Jest 抛出一个错误说 Invalid variable access theHookINeedToMock
。有谁知道正确的做法是什么?
此问题在 documentation:,
中有描述
A limitation with the factory parameter is that, since calls to jest.mock() are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration
在变量前加上 mock
前缀会禁用 Jest 检查。 let
或 const
变量在声明之前处于临时死区,在评估工厂时访问它们会导致未转译的 ES6 中出现运行时错误。对于急切评估的模拟模块,需要在工厂内部定义模拟。
避免这种情况的一种方法是使用提升的 var
声明并在工厂内初始化它:
var theFnIWantedToMock
jest.mock('../hooks', () => {
const actualHooks = jest.requireActual('../hooks')
theFnIWantedToMock = jest.fn()
return {
...actualHooks,
someHooks: jest.fn().mockReturnValue(theFnIWantedToMock),
}
})
保持对它的引用的一种方法是将其作为导入的一部分:
jest.mock('../hooks', () => {
const actualHooks = jest.requireActual('../hooks')
const theFnIWantedToMock = jest.fn()
return {
...actualHooks,
theFnIWantedToMock,
someHooks: jest.fn().mockReturnValue(theFnIWantedToMock),
}
})
这使得 theFnIWantedToMock
可以作为导入对象的一部分使用,也适用于 __mocks__
中的可重用模拟。
我想模拟一个函数并确保它已经执行了一定次数。 棘手的部分是我想要模拟的函数是另一个函数返回结果的一部分 我的实现大致是这样的
const theFnIWantedToMock = jest.fn()
jest.mock('../hooks', () => {
const actualHooks = jest.requireActual('../hooks')
return {
...actualHooks,
someHooks() {
return
{
theFnIWantedToMock,
}
}
}
})
describe('test', () => {
it('some test', () => {
//...
expect(theFnIWantedToMock).toHaveBeenCalledTimes(1)
})
})
但是 Jest 抛出一个错误说 Invalid variable access theHookINeedToMock
。有谁知道正确的做法是什么?
此问题在 documentation:,
中有描述A limitation with the factory parameter is that, since calls to jest.mock() are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration
在变量前加上 mock
前缀会禁用 Jest 检查。 let
或 const
变量在声明之前处于临时死区,在评估工厂时访问它们会导致未转译的 ES6 中出现运行时错误。对于急切评估的模拟模块,需要在工厂内部定义模拟。
避免这种情况的一种方法是使用提升的 var
声明并在工厂内初始化它:
var theFnIWantedToMock
jest.mock('../hooks', () => {
const actualHooks = jest.requireActual('../hooks')
theFnIWantedToMock = jest.fn()
return {
...actualHooks,
someHooks: jest.fn().mockReturnValue(theFnIWantedToMock),
}
})
保持对它的引用的一种方法是将其作为导入的一部分:
jest.mock('../hooks', () => {
const actualHooks = jest.requireActual('../hooks')
const theFnIWantedToMock = jest.fn()
return {
...actualHooks,
theFnIWantedToMock,
someHooks: jest.fn().mockReturnValue(theFnIWantedToMock),
}
})
这使得 theFnIWantedToMock
可以作为导入对象的一部分使用,也适用于 __mocks__
中的可重用模拟。