【发布时间】:2019-08-20 12:17:04
【问题描述】:
我的问题是关于如何模拟默认从另一个文件导入的 jest 函数。
我要测试的是该组件使用功能启用功能 (Features.js)
我使用 jest.fn() 模拟了这个函数,并尝试使用 mockReturnValueOnce
更改值如下所示。
模拟/features.js
export default function isFeatureEnabled (featureName) {
return true // want to test both true/false cases
}
test.spec.js
jest.mock('__mocks__/features.js', () => ({
isFeatureEnabled: jest.fn()
}))
describe('isFeatureEnabled', () => {
it('isFeatureEnabled=[true]', () => {
isFeatureEnabled.mockReturnValueOnce(true)
// some tests
})
it('isFeatureEnabled=[false]', () => {
isFeatureEnabled.mockReturnValueOnce(false)
// some tests
})
})
当我运行测试时,我收到了一个错误消息mockReturnValueOnce is not a function。这个stackoverflow question 启发了我以这种方式实现,但我仍然无法弄清楚如何使它工作。
【问题讨论】:
标签: reactjs unit-testing mocking jestjs