How to deal with requests made before msw has been activated?
See original GitHub issueIf I have a situation like this:
start('/msw.js')
fetch('/something-that-should-be-mocked')
MSW will not be registered as ready until after the request has already been forwarded along. What do you recommend for situations like this where the app is trying to make requests immediately?
Issue Analytics
- State:
- Created 4 years ago
- Comments:25 (20 by maintainers)
Top Results From Across the Web
Debugging uncaught requests - Recipes - Mock Service Worker
Verify the worker's scope ... A Service Worker can only intercept the requests under its scope. The maximum scope of the worker is...
Read more >Configuring Mock Service Worker (MSW) - codeburst
A request handler allows you to specify the method, path, and response when handling a REST API request. A response resolver is a...
Read more >Mock Service Worker - mockServiceWorker not listening ...
I have tried to running mockserviceworker in angular prod and dev build . i have kept debugger in self.addEventListener('message') , compiler ...
Read more >A Comprehensive Guide to Mock Service Worker (MSW)
We have to import the setupServer function from msw/node and pass the previously created request handlers to that function.
Read more >Mock Service Worker Tutorial | How to Mock Network Requests
Mock Service Worker makes the testing and development story very simple by mocking requests at the network layer.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This should be re-opened. The promise returned by
start
can resolve before the service worker is actually ready.You can observe this in my app which has MSW deployed:
abc
and password123
api-client.js:20 GET https://bookshelf.jk/api/me net::ERR_NAME_NOT_RESOLVED
I’ve been able to workaround this by adding a timeout of 100ms before making any requests. I need some way to wait until the service worker has not only installed but is also ready to start proxying requests.
Hey, @mitchconquer. Please, don’t worry, this format is perfectly fine. It’s my bad, I haven’t yet established a way to bootstrap scenarios with MSW in a sandbox form.
Regarding the issue, I can recommend to store the start function itself, rather than its return statement. In case there’s some dark magic in place, it’ll help us ensure that we call the
start()
at the expected time.I’d be surprised if this didn’t work. I have a few integration tests that rely on awaiting the
start()
Promise, so such setup should be a valid one.Let me know if this suggestion is of any help. I’d prefer if you don’t have to introduce timeouts to await the registration.