Axios interceptors are skipped if XMLHttpRequestInterceptor is applied
Hello. I am using this beautiful library for almost half a year. Today I've come up with such a interesting problem: my custom axios response interceptor does not work if @mswjs/interceptors XMLHttpRequestInterceptor is applied.
Project: React @mswjs/interceptors version: 0.22.15
My apisauce (axios) response interceptor code:
sauce.axiosInstance.interceptors.response.use(
(response) => {
console.log('response success');
return response;
},
async function (error) {
console.log('error');
// ... some magic ...
return Promise.reject(error);
}
)
Note that contents of those functions do not matter. If XMLHttpRequestInterceptor is applied, then logs do not appear in console. Meantime, XMLHttpRequestInterceptor works fine, requests introspection works.
I've also tried same functionality with pure axios lib (latest version). Same problem. If XMLHttpRequestInterceptor is applied, then my axios interceptor seems to be skipped.
If I understand it correctly, mswjs/interceptors is designed mostly for requests introspecting. So I guess that there should not be a problem if I have others interceptors in my project.
Edit №1: Problem appears even if I only call interceptor.apply() method and not adding any .on() callbacks.
Edit №2: Tried to rearrange the order in which the interceptors are applied. Seems like I can isolate a more general problem: no matter what the result of the query is, after it is intercepted by XMLHttpRequestInterceptor, axios will never know that request is complete (infinite loading).
https://github.com/mswjs/interceptors/issues/111#issue-852426648 this issue looks very similar
Hi, @RNEvok.
It's generally not advised to combine the Interceptors library (or MSW) with any other interceptors, since the latter often stub request-issuing modules (or themselves) at the highest layer possible, preventing this library from functioning correctly.
That being said, as I understand, Axios interceptors can be used for purposes other than mocking (like logging). In that regard, we should allow Axios to run its logic without interference.
Please, would you have some time to open a pull request, adding a test for this behavior? I do believe we already test against Axios but we could use an example with Axios interceptors. Once the test is failing, I can look into it and make sure the library works as expected. Thanks!
Closing due to the lack of a reproduction case / test.