我有以下模块我试图在Jest中测试:
// myModule.js
export function otherFn() {
console.log('do something');
}
export function testFn() {
otherFn();
// do other things
}
Run Code Online (Sandbox Code Playgroud)
如上图所示,这部分出口命名的功能和重要的testFn用途otherFn.
在Jest中我正在编写单元测试时testFn,我想模拟该otherFn函数,因为我不希望错误otherFn影响我的单元测试testFn.我的问题是我不确定最好的方法:
// myModule.test.js
jest.unmock('myModule');
import { testFn, otherFn } from 'myModule';
describe('test category', () => {
it('tests something about testFn', () => {
// I want to mock "otherFn" here but can't reassign
// a.k.a. can't do otherFn = jest.fn()
});
});
Run Code Online (Sandbox Code Playgroud)
任何帮助/见解表示赞赏.
sinon.spy有2个参数,对象和函数名.
我有一个如下所列的模块:
module.exports = function xyz() { }
我该如何定义间谍xyz?我没有要使用的对象名称.
思考?