how to test axios interceptors with jest

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 :

axios.interceptors.response.use(
    (res) => res,
    (error) => {
      if (
        error.response &&
        (error.response.status?.toString() === "400" ||
          error.response.status?.toString() === "403" ||
          error.response.status?.toString() === "404")
      ) {
        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(
            "The request was made but no response was received, check your network connection"
          )
        );
      } else Promise.reject(error);
    }
);

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.

I have tried:

  1. 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 mockResolvedValue.
  2. I have tried adding an interceptor to the mocked axios instance but that did not work too.

Thanks

226 thoughts on “how to test axios interceptors with jest”

  1. Hi there! Quick question that’s totally off topic. Do you know how to make your site mobile friendly?
    My web site looks weird when browsing from my iphone. I’m trying to find a theme or plugin that
    might be able to fix this problem. If you have any suggestions, please share.

    With thanks!

    Reply
  2. When I initially commented I appear to have clicked the -Notify me when new comments are
    added- checkbox and from now on whenever a comment is
    added I receive 4 emails with the exact same comment.
    There has to be a way you can remove me from that service?
    Thanks!

    Reply
  3. Its like you read my mind! You seem to know so much about this,
    like you wrote the book in it or something. I think that you can do with some pics
    to drive the message home a little bit, but other than that,
    this is excellent blog. A fantastic read. I’ll certainly be
    back.

    Reply
  4. Great beat ! I would like to apprentice whilst
    you amend your website, how can i subscribe for a weblog web site?
    The account aided me a appropriate deal. I were a little bit familiar of this your broadcast provided brilliant transparent idea

    Reply
  5. Pretty section of content. I just stumbled upon your
    weblog and in accession capital to assert that I acquire actually enjoyed account your blog posts.

    Any way I will be subscribing to your augment and even I achievement you access consistently quickly.

    Reply
  6. Right here is the perfect site for anyone who
    hopes to find out about this topic. You realize so much its almost tough to argue with you (not that I personally will
    need to…HaHa). You definitely put a new spin on a subject that’s been discussed for ages.
    Great stuff, just great!

    Reply

Leave a Comment