title: how to test axios interceptors with jest
body: <p>In my project, I have a namespace that exports some functions that use Axios, in the same file I add an interceptor to axios instance like that :</p>
  <pre class="lang-js prettyprint-override"><code>axios.interceptors.response.use(
      (res) =&gt; res,
      (error) =&gt; {
        if (
          error.response &amp;&amp;
          (error.response.status?.toString() === &quot;400&quot; ||
            error.response.status?.toString() === &quot;403&quot; ||
            error.response.status?.toString() === &quot;404&quot;)
        ) {
          return Promise.reject(
            Error(JSON.stringify(error.response.data?.status?.errors[0]))
          );
        } else if (error.response) {
          return Promise.reject(
            Error(
              `server responsed with the following code: ${error.response?.status} and the following message: ${error.response?.statusText}`
            )
          );
        } else if (error.request) {
          return Promise.reject(
            Error(
              &quot;The request was made but no response was received, check your network connection&quot;
            )
          );
        } else Promise.reject(error);
      }
  );
  </code></pre>
  <p>I want to test that this interceptor works as expected, I search the forms here and googled a lot but all the answers are basically mocking the interceptor not testing it.</p>
  <p>I have tried:</p>
  <ol>
  <li>mocking the response of an axios post request and checking the AxiosPromise that gets returned but it only contained the result I mocked. it seems that it ignores the interceptor when I mock using <code>mockResolvedValue</code>.</li>
  <li>I have tried adding an interceptor to the mocked axios instance but that did not work too.</li>
  </ol>
  <p>Thanks</p>