【发布时间】:2019-05-19 05:14:09
【问题描述】:
我在使用手动 Jest 模拟时遇到了一些问题,需要一些帮助。
我正在测试的文件如下所示:
import withTranslation from '../utils/withTranslation'
withTranslation('test')
我在这样的测试文件中模拟一个模块。
import withTranslation from '../utils/withTranslation'
jest.mock('../utils/withTranslation')
// tests here
我在../utils/__mocks__/withTranslation.js 有手动模拟,代码如下:
const impl = (...args) => {
console.log('in mock', args)
return args
}
export default impl
当测试运行时,使用了 mock,我可以看到控制台日志。到目前为止一切顺利。
但是,我希望能够在 withTranslation 被嘲笑时对它的使用做出断言。例如,expect(withTranslation).toHaveBeenCalledWith('test')。
因此,我将手动模拟更改为 Jest 模拟函数,其实现与以前相同。
const impl = (...args) => {
console.log('in mock 1', args)
return args
}
// only difference is wrapping in jest.fn()
export default jest.fn(impl)
现在运行测试时,使用了一个通用的 Jest 模拟函数,我看不到控制台日志,所以我的假实现永远不会被调用。有什么想法吗?
【问题讨论】:
标签: node.js unit-testing jestjs