简体   繁体   English

开玩笑的嘲笑诺言方法被称为错误的次数

[英]jest mocking promise method called wrong number of times

As part of my redux action, it makes several sequential api requests. 作为我的redux动作的一部分,它发出了几个顺序的api请求。 The apiCall method returns a Promise with some value, and that value is used by a subsequent apiCall to make another request, and so on. apiCall方法返回带有一些值的Promise,随后的apiCall使用该值发出另一个请求,依此类推。 I'm using Jest to test these api calls. 我正在使用Jest测试这些api调用。

 const myAPI = { apiCall(param: any): Promise<any> { return new Promise((resolve, reject) => { resolve('result'); }); }, }; const serialAPIRequests = () => { myAPI.apiCall('first_param') .then((result) => { console.log(result); return myAPI.apiCall(result); }) .then((result) => { console.log(result); return myAPI.apiCall(result); }) .then((result) => { console.log(result); return Promise.resolve(result); }); }; 

I am trying to write a test to ensure apiCall has been called the correct number of times and with the right parameters. 我试图编写一个测试以确保apiCall已被调用正确的次数并具有正确的参数。

 describe.only('my test', () => { it('should test api stuff', () => { myAPI.apiCall = jest.fn() .mockReturnValueOnce(Promise.resolve('result1')) .mockReturnValueOnce(Promise.resolve('result2')) .mockReturnValueOnce(Promise.resolve('result3')); serialAPIRequests(); expect(myAPI.apiCall).toHaveBeenCalledTimes(3); }); }); 

What happens is that Jest report Expected mock function to have been called three times, but it was called one time. 发生的事情是,Jest report Expected mock function to have been called three times, but it was called one time.

Jest test result shows that 开玩笑的测试结果表明

  ● Console

console.log 
  result1
console.log 
  result2
console.log 
  result3

 ● my test › should test api stuff

expect(jest.fn()).toHaveBeenCalledTimes(3)

Expected mock function to have been called three times, but it was called one time.

The fact that console.log showed different values means the mocked return was properly passed through the mock function and it was called 3 times. console.log显示不同值的事实意味着模拟返回已通过模拟函数正确传递,并且被调用了3次。

What could be causing this and how do I properly test this function? 是什么原因造成的?如何正确测试此功能?

Promises are async so by the time you do you check the mock was actually called once. 承诺是异步的,因此当您检查该模拟实际上被调用过一次时,就可以了。

You could do this instead. 您可以这样做。 Wait for all calls to be done and return a promise to indicate the test is async. 等待所有调用完成并返回承诺以表明测试是异步的。

return serialAPIRequests().then(() => {
    expect(myAPI.apiCall).toHaveBeenCalledTimes(3);
})

Use async/await to test async code. 使用async / await测试异步代码。 Read more here: https://facebook.github.io/jest/docs/en/tutorial-async.html 在此处阅读更多信息: https : //facebook.github.io/jest/docs/en/tutorial-async.html

describe.only('my test', () => {
      it('should test api stuff', async () => {
        myAPI.apiCall = jest.fn()
          .mockReturnValueOnce(Promise.resolve('result1'))
          .mockReturnValueOnce(Promise.resolve('result2'))
          .mockReturnValueOnce(Promise.resolve('result3'));
        await serialAPIRequests();
        expect(myAPI.apiCall).toHaveBeenCalledTimes(3);
      });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM