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