I have an interceptor set up in Axios that will refresh tokens and then retry the call if the initial HTTP response is a 401.
I'm trying to unit test this logic by mocking Axios request to return a 401 and then a valid response (see a simplified version below) and asserting that it gets called twice.
My problem is that because I'm mocking request the interceptor doesn't seem to run. Rather than retry the request, I get the stubAuthError returned.
Is there any way of achieving what I'm after, or is there a better way to test this?
index.js
const axios = require('axios');
axios.interceptors.response.use(
response => response,
error => {
const status = error.response ? error.response.status : null;
if (status === 401) {
return makeRequest();
} else {
return Promise.reject(error);
}
}
);
async function makeRequest() {
return axios.request('url');
}
module.exports = {
makeRequest
};
index.spec.js
const axios = require('axios');
const { makeRequest } = require('./index');
const stubAuthError = { response: { status: 401 } };
const stubResponse = { data: { foo: 'bar' } };
it('should make the request twice', async () => {
expect.assertions(1);
axios.request = jest.fn();
axios.request
.mockRejectedValueOnce(stubAuthError)
.mockResolvedValueOnce(stubResponse);
try {
await makeRequest();
} catch (error) {}
expect(axios.request).toHaveBeenCalledTimes(2);
});