0

I have a React app which I am testing with Testcafe (with gherkin-testcafe). I am using MSW to mock API responses which works great. However the issue is when running testcafe tests against my local version running with MSW I get the following message.

MSW Cannot intercept requests on this page because it's outside of the worker's scope ("http://localhost:1337"). If you wish to mock API requests on this page, you must resolve this scope issue.

 - (Recommended) Register the worker at the root level ("/") of your application.
 - Set the "Service-Worker-Allowed" response header to allow out-of-scope workers.

As far as I can tell MSW defaults to using scope "/" if you don't set one anyway and I have tried setting it explicitly and seemed to make no difference. I have also tried setting the header but I don't really know what I meant to set it to. I tried "/"

Does anyone know how I can this to work?

GaryB
  • 1

1 Answers1

0

The error implies that the worker is not registered at the root. Note that a service worker defaults to the scope of the directory from where it's being served, and that directory effectively becomes its /. It doesn't mean it's the root of your application.

Consider this:

public/
  foo/
    mockServiceWorker.js

The / scope for the worker will be /foo (given your site is served from /public). This may be the issue in your case.

To resolve this:

  1. Make sure that you've generated the worker file in the root level of your application. Usually, it's the /public folder.
  2. Check the URL location of the page where you see this warning. This is a browser-only warning so you can inspect your runtime—it contains the most useful information to debug this. Perhaps Testcafe nests your entire app under some path, and that's what breaks the worker functionality. If that's the case, either read Testcafe docs to serve your app at the root route, or configure the serviceWorker.scope respectively, so the worker knows at which path it's being served (this may also require proxying the worker script to the Testcafe's location as a part of the testing setup).

If nothing helps, provide us with the list of things you've tried and what was the result you received.

kettanaito
  • 974
  • 5
  • 12