我有一个具有两个功能的模块。
const exampleFunctionOne = () => { };
const exampleFunctionTwo = () => { };
在我的测试文件中,我像这样模拟 exampleFunctionOne:
let mockExampleFunctionOne = () => {};
jest.mock('../../functions/common/helper', () => ({
exampleFunctionOne: jest.fn().mockImplementation(() => mockExampleFunctionOne()),
}));
我想使用exampleFunctionTwo的真实实现。我该如何调整我的代码来做到这一点?
答案 0 :(得分:0)
如果您尝试模拟模块的某些部分,我建议您在测试文件中导入模块本身,然后覆盖其公共功能。
const helperFunctions = require('../../functions/common/helper');
然后你可以简单地覆盖一些导出的函数:
helperFunctions.exampleFunctionOne = jest.fn().mockImplementation(() => mockExampleFunctionOne());
这样,“exampleFunctionTwo”保持不变,而您只是在嘲笑“exampleFunctionOne”。
如果您最终想要监视模拟函数(例如断言“exampleFunctionOne”正在调用它),您可以监视它:
const exampleFunctionOneSpy = jest.spyOn(helperFunctions, 'exampleFunctionOne');
// ...rest of the test here
expect(exampleFunctionOneSpy).toBeCalled();