【发布时间】:2020-08-20 10:09:36
【问题描述】:
我正在尝试使用 Jest 和 Typescript 模拟对象的函数,但它不起作用。这是我所拥有的简短版本:
// myModule.ts
export const Foo = {
doSomething: () => {
// ... does something
console.log('original implementation');
}
}
然后在我的测试中:
jest.mock('.myModule.ts', () => {
return {
Foo: {
doSomething: jest.fn().mockImplementation(() => {
console.log('mock implementation')
})
}
}
})
// .. further down in the test
Foo.doSomething();
当我调用Foo.doSomething() 时,不应该调用console.log('mock implementation') 吗?它不会出错,也不会再调用原始实现,但也不会调用我的 mockImplementation。
【问题讨论】:
标签: typescript unit-testing jestjs