title: jest mocking promise method called wrong number of times
body: <p>As part of my redux action, it makes several sequential api requests. The <code>apiCall</code> method returns a Promise with some value, and that value is used by a subsequent <code>apiCall</code> to make another request, and so on. I'm using Jest to test these api calls.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const myAPI = {
    apiCall(param: any): Promise&lt;any&gt; {
      return new Promise((resolve, reject) =&gt; {
        resolve('result');
      });
    },
  };
  
  const serialAPIRequests = () =&gt; {
    myAPI.apiCall('first_param')
      .then((result) =&gt; {
        console.log(result);
        return myAPI.apiCall(result);
      })
      .then((result) =&gt; {
        console.log(result);
        return myAPI.apiCall(result);
      })
      .then((result) =&gt; {
        console.log(result);
        return Promise.resolve(result);
      });
  };</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am trying to write a test to ensure <code>apiCall</code> has been called the correct number of times and with the right parameters.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>describe.only('my test', () =&gt; {
    it('should test api stuff', () =&gt; {
      myAPI.apiCall = jest.fn()
        .mockReturnValueOnce(Promise.resolve('result1'))
        .mockReturnValueOnce(Promise.resolve('result2'))
        .mockReturnValueOnce(Promise.resolve('result3'));
      serialAPIRequests();
      expect(myAPI.apiCall).toHaveBeenCalledTimes(3);
    });
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>What happens is that Jest report <code>Expected mock function to have been called three times, but it was called one time.</code></p>
  
  <p>Jest test result shows that</p>
  
  <pre><code>  ● 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.
  </code></pre>
  
  <p>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.</p>
  
  <p>What could be causing this and how do I properly test this function?</p>