mswjs / examples

Examples of Mock Service Worker usage with various frameworks and libraries.
683 stars 211 forks source link

Question: Is it possible to intercept ALL requests? #95

Closed onlywei closed 10 months ago

onlywei commented 10 months ago

MSW seems to require me to write down a very specific URL in order to capture my request in Jest. I just want to set up a server to capture every request regardless of the URL and then. turn the server off after my tests are finished.

I've tried the following:

None of these have worked.

kettanaito commented 10 months ago

Hi, @onlywei.

Yes, you can intercept all HTTP requests with MSW by using the http.all() request handler. It's designed to allow request method-agnostic interception. You can both provide a specific path or provide an wildcard to capture all requests:

http.all('*', ({ request, cookies }) => {
  // Handle ALL requests your application makes.
})
onlywei commented 10 months ago

Hi @kettanaito. Thank you for the reply. I don't think that was my question though. I'm not worried about request methods. What I'm experiencing is that the wildcard "*" isn't capturing my request. I still get this:

[MSW] Warning: captured a request without a matching request handler:

Even when I use .all('*'...

kettanaito commented 10 months ago

@onlywei, I mentioned methods because request interception is method first, path second.

If there are still requests that don't match http.all(), I highly recommend following the Debugging runbook. Let me know how far you get there.