[英]jest.mock how to find whether the mocked function was called or not
I m mocking a module like this:我正在嘲笑这样的模块:
jest.mock('@/src/myModule.ts', () => ({
load: jest.fn(),
delete: jest.fn(),
}));
In my test how I can check whether load was called or not?在我的测试中,如何检查负载是否被调用?
it('some test', () => {
const x = someFunction(); // this method is calling myModule.ts/load
expect(???);
});
The point is I cannot reference load from outside, so if I try this关键是我不能从外部引用负载,所以如果我尝试这个
const objs = {
load: jest.fn(),
delete: jest.fn(),
};
jest.mock('@/src/myModule.ts', () => objs);
and then try to refer 'objs', its not working然后尝试引用'objs',它不起作用
...
expect(objs.load).toBeCalledTimes(1); // failed
...
const loadMock = jest.fn()
jest.mock('@/src/myModule.ts', () => ({
load: () => loadMock(),
delete: jest.fn(),
}));
In your test you can check whether load was called or not:在您的测试中,您可以检查是否调用了负载:
it('some test', () => {
const x = someFunction(); // this method is calling myModule.ts/load
expect(loadMock).toHaveBeenCalled();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.