1 回答
TA贡献1806条经验 获得超8个赞
所以最好的方法是检查结果而不是专门检查函数调用。
实际在做什么checkData(您尚未显示)。它是否在另一个文件中调用某些内容?
如果是这样,请在另一个文件中模拟该函数以返回一些数据并验证在您挂载组件时是否调用了模拟函数。
例如:
// component file
import { someMethod } from 'someModule';
export class MyComponent extends React.Component {
async checkData() {
await someMethod();
}
componentDidMount() {
this.checkData();
}
render() {
}
}
// in your spec file
import { someMethod } from 'someModule';
jest.mock('someModule');
someMethod.mockImplementation(() => {
// do whatever you want here
});
// do your all your normal setup, probably something like this
let mountedComponent;
beforeEach(() => {
mountedComponent = mount(...);
});
// clear the mock after each mount
afterEach(() => someMethod.mockClear());
it('should do things',() => {
expect(someMethod).toHaveBeenCalled();
});
添加回答
举报
