interceptors
interceptors copied to clipboard
fix: abort pending requests when the interceptor is disposed
This PR aims to solve issue https://github.com/mswjs/msw/issues/778.
I took the following approach :
- Decorate the global AbortController class
- Hold weak references to all controllers created
- Once a request is intercepted, hold a strong reference to its controller
- Once a request ends, free the strong reference
- On dispose, abort the requests using their controllers
Unhandled requests are not affected.
Since this only affects Node.js, I implemented the logic into FetchInterceptor
and ClientRequestInterceptor
Let's have a discussion on this one, I think I know how we can approach it in an elegant manner. Will gather my thoughts and post here once I have a minute.
@kettanaito do you think you can find some time in the coming weeks to handle this PR ? I think I can allocate an afternoon next week if you want to discuss this.
Wanted to voice my support for this. As being able to test abort controllers is an important part of unit testing. Hoping this PR solves https://github.com/mswjs/msw/discussions/1646
Copying my 2cents here as well...
I think this change really should be in the RequestHandler
not the interceptors.
Check out mswjs/msw#778 for a more full discussion.
Wanted to voice my support for this. As being able to test abort controllers is an important part of unit testing. Hoping this PR solves https://github.com/mswjs/msw/discussions/1646
@howagain MSW 2.0 includes my first PR that fixes AbortSignal. So if you manually abort the request, you should be able to test that. If not, this PR won't be enough because what this PR does is to abort all requests during MSW shutdown. It gives you no control while the tests are running. You may workaround this if you stop and start the server during the test itself, which introduces new issues, if you run yours tests concurrently for example.
This could be a nice improvement to build on top of this PR. Although, given that this PR has not been merged after 5 months, I will not go further before it has been merged.
Maintainers have a limited bandwidth. I understand it can be frustrating but we are working hundreds of hours for the benefits of strangers on the internet. For free.
Be kind ❤️