Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow aborting pending requests #398

Closed
kettanaito opened this issue Aug 7, 2023 · 2 comments
Closed

Allow aborting pending requests #398

kettanaito opened this issue Aug 7, 2023 · 2 comments

Comments

@kettanaito
Copy link
Member

kettanaito commented Aug 7, 2023

Motivation

When working with MSW, users experience an issue that requests may continue pending even after the tests are done. The issue itself is often caused by the user's mistake, but it's still wrong for us to keep requests alive after server.close()/interceptor.dispose() was called.

I'd be careful not to introduce "abort on close" behavior in the Interceptors, because it's not unexpected to dispose of the interceptor while still expecting the request to finish. I'm not sure at the moment what would be the default behavior here, if any.

Proposal

Some sort of unified abort API would be nice to expose from the Interceptors. I can imagine something like this:

// consumer.js
const interceptor = new BatchInterceptor({ interceptors: [...] })

interceptor.on('request', ({ request, controller }) => {
  // Unified means to abort any intercepted request.
  controller.abort()
})

Technical details

All currently supported request modules provide native means to achieve the abort functionality:

Technically speaking, this task is about propagating those methods up the interceptor chain, wrapping them in a nice API, and exposing that API in the argument of request listeners.

Here's a draft of the controller interface:

interface RequestController {
  abort(reason?: unknown): void
}

XMLHttpRequest.abort() doesn't support the reason parameter but that's okay.

I'm also considering whether we should move .respondWith() to the newly introduced controller. That way, the controller is what allows the consumer to control what to to with the intercepted request.

@gxxcastillo
Copy link

The issue itself is often caused by the user's mistake

What are the mistakes that users often make?

...asking for a friend

@kettanaito
Copy link
Member Author

Will keep this in mind and close in favor of #520. There are a few corner cases that make the experience around aborting requests unpredictable. I'd love to discuss those before moving forward with this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants