Jestjs 如何使用Jest计算对循环承诺函数的调用
我有一个功能:Jestjs 如何使用Jest计算对循环承诺函数的调用,jestjs,Jestjs,我有一个功能: myFunc = async () => { for (let item of items) { await doSomething(items); } } 我使用for of循环,因为它尊重wait 我的测试: it('should call doSomething twice', () => { const doSomething = jest.fn(); const items = [{a: 'a'}, {b: 'b'}]; m
myFunc = async () => {
for (let item of items) {
await doSomething(items);
}
}
我使用for of
循环,因为它尊重wait
我的测试:
it('should call doSomething twice', () => {
const doSomething = jest.fn();
const items = [{a: 'a'}, {b: 'b'}];
myFunc(items);
expect(doSomething).toBeCalledTimes(2);
})
它失败,因为doSomething只调用一次。您需要等待所有迭代完成 试着这样做:
it('应该调用doSomething两次',async()=>{
const doSomething=jest.fn();
常量项=[{a:'a'},{b:'b'}];
//等待
等待myFunc(项目);
期望(剂量)达到要求的时间(2);
})