Testing axios interceptors

This issue has been tracked since 2018-01-15.

Hello,

I would like to test axios interceptors.
Is there a way to do this with moxios?

I currently have this interceptor:

axios.interceptors.response.use(null, (error) => {
  if (error.response.status === 401) {
    window.location.assign(`${error.response.data.loginUrl}/?redirect=${encodeURIComponent(window.location.href)}`);
  }
  return Promise.reject(error);
});

And have a test like so:

test('should redirect if 401', (done) => {
  window.location.assign = jest.fn();

  moxios.withMock(function () {
    axios.get('/foo');

    moxios.wait(async () => {
      const request = moxios.requests.mostRecent();
      await request.respondWith({
        status: 401,
        response: {
          data: {
            loginUrl: 'hello.world'
          }
        }
      });
      expect(window.location.assign).toHaveBeenCalled();
      done();
    });
  });
});

This throws me: Error: Request failed with status code 401.
And any console.log in the interceptor isn't displayed.

Any idea how I could proceed?

Thank you!

anilanar wrote this answer on 2018-05-27

Kamino cloned this issue to anilanar/moxios

More Details About Repo
Owner Name axios
Repo Name moxios
Full Name axios/moxios
Language JavaScript
Created Date 2016-05-25
Updated Date 2022-12-05
Star Count 1386
Watcher Count 20
Fork Count 85
Issue Count 19

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date