【发布时间】:2020-01-09 02:14:05
【问题描述】:
我可以使用jest.fn(() => 1) 或() => 1 创建一个模拟函数。即使我不需要它,我也应该做第一个吗? (例如我没有使用expect().toHaveBeenCalled())
const foo = ({ helper }) => helper()
test('example v1', () => {
const arg = {
helper: jest.fn(() => 1)
}
expect(foo(arg)).toBe(1)
})
test('example v2', () => {
const arg = {
helper: () => 1
}
expect(foo(arg)).toBe(1)
})
始终使用jest.fn() 的可能好处包括:
- 文档——知道它应该是一个模拟函数
- 以后方便添加
expect(arg.helper).toHaveBeenCalled*()
【问题讨论】: