mswjs / msw

Seamless REST/GraphQL API mocking library for browser and Node.js.
https://mswjs.io
MIT License
15.81k stars 515 forks source link

ServiceWorker is not registered at the second test in Testcafe #1508

Closed Roy412 closed 2 weeks ago

Roy412 commented 1 year ago

Prerequisites

Environment check

Browsers

Chromium (Chrome, Brave, etc.)

Reproduction repository

https://github.com/Roy412/msw-testcafe-error/tree/msw_mock_issue

Reproduction steps

The second test fails because MSW service worker is not registered. https://github.com/Roy412/msw-testcafe-error/blob/5ba82e4e7ce516caab9ad551691db83a0e5737d3/app/pages/_app.js#L18

Screenshot 2022-12-27 at 10 22 24 AM

Current behavior

There're two tests that are exactly the same. The MSW service worker is registered successfully at the first test while it is not enabled for the second one. When there is only one test, it passes successfully and from the second test, it will fail. On the second test, following code returns the multiple available existingStates. https://github.com/mswjs/msw/blob/7380011de47460212065ca41be1b5a1c60ed1b98/src/setupWorker/start/utils/getWorkerByRegistration.ts#L17

Screenshot 2022-12-27 at 12 28 22 PM

And it returns the first one which was used at the first test. It should return the second one.

Expected behavior

The MSW service worker should be registered correctly for the second test too.

kettanaito commented 2 weeks ago

That script URL looks broken to me. The worker should be registered at http://localhost:64812/mockServiceWorker.js once to affect all the nested tests (notice that each test is served under a randomly generated path. Instead, it looks like each test serves its own worker, and registers it only under the test's path.

Please refer to the TestCafe's docs to learn how to serve the worker script at the root of your test application. There has also been a ton of changes to MSW since this issue was filed, so please upgrade the library before trying anything else. Thanks.