question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to deal with requests made before msw has been activated?

See original GitHub issue

If 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:closed
  • Created 4 years ago
  • Comments:25 (20 by maintainers)

github_iconTop GitHub Comments

4reactions
kentcdoddscommented, Mar 27, 2020

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:

  1. https://5e7d73d0938fb965a65bbdf3--the-react-bookshelf.netlify.app
  2. Register with username abc and password 123
  3. Do a “hard refresh” via <kbd>⌘</kbd> + <kbd>⇧</kbd> + <kbd>r</kbd>
  4. Notice error in the app and console: 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.

2reactions
kettanaitocommented, May 6, 2020

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.

// mocks.js
const worker = setupWorker(...)
window.__mswStart = worker.start // notice the reference
// apiClient.js
async function apiClient() {
  if (__DEV__) {
    await window.__mswStart()
  }
}

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.

Going back to sandboxing examples, I’ve tried to establish a template in Codesandbox, yet for some reason it fails to register the worker. To be more precise, it ignores the registration attempt, when I reference the service worker file relatively. If I give it an absolute URL (that includes the sandbox URL), it registers just fine. Haven’t figured a reliable sandbox template just yet.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found