【发布时间】:2021-07-17 23:52:33
【问题描述】:
我想模拟这个,所以我只模拟 Bar 的 get() 返回方法,所以当 Foo 调用 find() 时, get() 被模拟并返回。
条形文件
// bar file
function Bar(){
function get(){
//...
}
return {
get
}
}
export default Bar;
Foo 文件
// foo file
import Bar from './bar';
function Foo(func){
const bar = Bar();
function find(){
bar.get();
}
return {
find,
};
}
export default Foo
索引.js
// index.js
import Bar from './bar';
import Foo from './foo';
export {Bar, Foo};
我的测试
//foo test
import {Bar, Foo} from './index'
//This mocks everything
jest.mock('./index');
// This will say Cannot find module Bar
jest.mock('Bar', () => ({
get: () => jest.fn().mockReturnValue('test'),
}));
describe('test foo', ()=> {
let foor = Foo();
foo.find();
})
我尝试模拟 Bar 本身,它说“找不到模块 Bar”
如何模拟 Bar 这个工厂函数?
【问题讨论】:
标签: unit-testing jestjs