title: Mocking resolved values when multiple calls are made to Axios from a single method
body: <p><strong>Background:</strong>
  I have a series of methods that will together make multiple axios calls. In my scenario I must make the first axios call to get some data that I can then use to generate the second axios call. After that I have the third axios call that may be unrelated to the first two.</p>
  <p>In my test I must validate that the URL used by the last axios call be a certain URL</p>
  <p><strong>Problem:</strong>
  When I validate the URL, I get only the first axios call. From some troubleshooting on my end, it seems as if Jest only executes the first axios call or that my resolved values are not being interpreted properly.</p>
  <p><strong>Ask:</strong>
  Is there a way what I'm trying to do in my scenario can work? Any help is greatly appreciate.</p>
  <p>Here is some of the code that I've sanitized the best I can.</p>
  <p><strong>Main program:</strong></p>
  <pre><code>interface IPrerequisite {
    // Some implementation
  }
  
  class Prerequisite implements IPrerequisite {
    error = &quot;&quot;;
    async checkStatus(token: string) {
      return Promise.resolve(true);
    }
  
    async getAxiosResponse(url: string, token: string) {
      const headers: AxiosRequestConfig = {
        headers: {
          Accept: &quot;application/json&quot;,
          &quot;Content-Type&quot;: &quot;application/json&quot;,
          Authorization: `Bearer ${token}`,
        },
        timeout: timeoutDuration,
      };
  
      return axios
        .post(url, {}, headers)
        .then((res: AxiosResponse) =&gt; {
          return Promise.resolve(res.data);
        })
        .catch((err: AxiosError) =&gt; {
          return Promise.resolve(&quot;&quot;);
        });
    }
  }
  
  class ListKeysPrereq extends Prerequisite {
    constructor() {
      super();
    }
    async checkStatus(token: string) {
      // First axios request
      const firstResponse = await this.getAxiosResponse(url, token);
      if (firstResponse === &quot;&quot;) {
        return Promise.resolve(false);
      }
  
      // Second axios request
        // Do some stuff with first response to pass to second request
      const secondResponse = await this.getAxiosResponse(url, token);
      if (secondResponse === &quot;&quot;) {
        return Promise.resolve(false);
      }
  
      // Do some stuff with second response
  
      return Promise.resolve(true);
    }
  }
  
  class PrerequisiteFactory {
    static getInstance(prerequisite: string): IPrerequisite {
      switch (prerequisite) {
        case &quot;LIST_KEYS_PREREQ&quot;:
          return new ListKeysPrereq();
        default:
          return null;
      }
    }
  }
  
  const checkConnectionPrerequisites = async (token: string) =&gt; {
    enum Prerequisites {
      LIST_KEYS_PREREQ,
    }
  
    for (const prereq in Prerequisites) {
      if (isNaN(Number(prereq))) {
        const prerequisite = PrerequisiteFactory.getInstance(prereq);
        const prerequisiteStatus = await prerequisite.checkStatus(token);
        if (!prerequisiteStatus) {
          return Promise.resolve(prerequisite.error);
        }
      }
    }
  
    return Promise.resolve(&quot;&quot;);
  };
  
  const getConnectionString = async (url: string, token: string, location: string): Promise&lt;string&gt; =&gt; {
    try {
      const prereqStatus = await checkPrerequisites(token);
      // Do some stuff
    } catch (err) {
      // Handle exception
    }
  
    // Last axios request
    return axios
      .post(url, {}, headers)
      .then((response: AxiosResponse) =&gt; {
        // Do something with the last response
        return Promise.resolve(&quot;sometext&quot;);
      })
      .catch((err: AxiosError) =&gt; {
        handleError(operationName, err);
        return Promise.resolve(&quot;&quot;);
      });
  };
  
  export const startProgram = async (token: string) =&gt; {
    connectionString = await getConnectionString(url, token, location);
  
  };
  </code></pre>
  <p><strong>Jest Test</strong></p>
  <pre><code>const postMock: jest.Mock = jest
    .fn()
    .mockResolvedValueOnce({
      status: 200,
      data: {
        someUri: &quot;https://somefakestorageaccount.blob.core.contoso.com:443/somefakecontainer/somefakeblob&quot;,
      },
    })
    .mockResolvedValueOnce({ status: 200, data: { connectionString: &quot;wss://thisfakestring/&quot; } })
    .mockResolvedValueOnce({ status: 400 })
    .mockResolvedValue({ status: 200, data: { connectionString: &quot;wss://thisfakestring/&quot; } });
  
  jest.mock(&quot;axios&quot;, () =&gt; {
    return {
      post: postMock,
    };
  });
  
  test(&quot;Sample Test&quot;, () =&gt; {
      const assertedUrl = &quot;https://assertedurl.constoso.com/somecontainer/someblob&quot;;
      main.startprogram(&quot;token&quot;).then(() =&gt; {
        expect(postMock).toHaveBeenCalledTimes(1);
        expect(postMock.mock.calls[0][0]).toBe(assertedUrl); // This returns the call from only the first axios call
        expect(loadMock).toHaveBeenCalledTimes(4);
        expect(openMock).toHaveBeenCalledTimes(2);
      });
    });
  </code></pre>