我正在尝试模拟一个模块(该模块具有导出的功能[并且也对此进行了模拟])。
我想监视导出的函数以检查它是否被调用。
这是我的代码...
import { addNewPaymentMethod } from '../src/service'
jest.mock('../src/service', () => ({
addNewPaymentMethod : (paymentMethodInfoModel) => {
let responseFromApi = {responseStatus:{name:'blah'}};
return Promise.resolve(responseFromApi);
}
}))
import { createNewPaymentMethod } from '../src/actions/paymentMethod'
test('test expect', () => {
createNewPaymentMethod({owNickName:'nName',id:22})();
//this is the bit I don't know how to do
//...
jest.spyOn(addNewPaymentMethod);
expect(addNewPaymentMethod).toBeCalledWith({some:object});
});
答案 0 :(得分:0)
您可以使用jest.fn() 。 mockResolvedValue(value)
定义模拟。spark.yarn.app.container.log.dir
,然后因为它是一个jest.fn(),所以您不需要spyOn它:) 但请注意,由于它是异步的(使用promise),因此您必须先链接到该方法(然后使用return):
jest.mock('../src/service', () => ({
addNewPaymentMethod : jest.fn().mockResolvedValue({
responseStatus: {
name:'blah'
}
})
}))
或使用异步/等待:
it('should have been called with data', () => {
const data = {};
return service.methodWhichCallsDependencysaddNewPaymentMethod(data).then(() => {
return expect(addNewPaymentMethod).toHaveBeenCalledWith(data);
});
});
我已经设置了a working example