如何模拟模块中的特定功能
How to mock specific function in a module
假设我在 module.js
中有一个方法 bestFunction
:
//module.js
export const bestFunction = x => {
return x + 1
}
如果我的代码本身是这样导入的,我该如何模拟 bestFunction
:
import { bestFunction } from './module.js'
在你的测试中:
import * as depends from './module.js';
describe("when testing..", () => {
beforeEach(() => {
depends.bestFunction = jest.fn().mockImplementation(() => 22);
})
it ('doStuff', () => {
expect(objectUnderTest.foo()).toEqual('22 red balloons');
});
});
假设我在 module.js
中有一个方法 bestFunction
:
//module.js
export const bestFunction = x => {
return x + 1
}
如果我的代码本身是这样导入的,我该如何模拟 bestFunction
:
import { bestFunction } from './module.js'
在你的测试中:
import * as depends from './module.js';
describe("when testing..", () => {
beforeEach(() => {
depends.bestFunction = jest.fn().mockImplementation(() => 22);
})
it ('doStuff', () => {
expect(objectUnderTest.foo()).toEqual('22 red balloons');
});
});