带有笑话的模拟模块导出功能



我有一个文件lib.ts

export const getValue() { return 'original value'; }
export const callGetValue() { return getValue(); }

测试文件lib.spec.ts

import * as lib from './lib';
// ...
it('works', () => {
jest.spyOn(lib, 'getValue').mockImplementation( () => 'new value');
expect(lib.callGetValue()).toBe('new value'); // it's not!
});
// ...

我想嘲笑getValue()并让它返回'new value'.不是,为什么?!

不幸的是,这实际上是不可能的,请参阅 https://github.com/facebook/jest/issues/936 中的讨论

最新更新