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.

MSW seems to be unreliably working with Cypress

See original GitHub issue

Hi there,

after struggling for too long with this … asking here

Describe the bug

The worker sometimes starts sometimes not, and the request are sometimes intercepted others not.

This only happen when the app is tested with cypress

I’m only using a simple cy.visit('/')

and the console sometimes shows the

image

sometimes not

other times it show the worker has started but the requests are not intercepted

ie. this does not show

image

I’m starting the worker like this :

if (process.env.NODE_ENV === 'development') {
  const { worker } = require('./services/mocks');
  worker.start();
}

Note: the requests I’m mentioning are requests that are sent when the React App (CRA) starts

Environment

  • msw:0.19.4
  • nodejs: 14.1
  • npm: 6.14

Please also provide your browser version.

chrome 83

Expected behavior

Consistent starting and interceptin of requests while working w/ Cypress

thanks a lot

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
MaxDNGcommented, Jul 13, 2020

Hi All,

I have been facing the same issue. MSW indicated it was enabled but the requests were only caught with a force refresh and when the service worker was installed.

It turns out the issue was likely coming from the default registerServiceWorker provided in create-react-app. It was set to serviceWorker.unregister() and thus was calling

navigator.serviceWorker.ready.then(registration => {
  registration.unregister();
});

I’m no expert in service workers but I assume it was messing up with MSW.

TL;DR: remove or comment out the default registerWorker from CRA:

// import * as serviceWorker from './serviceWorker.js';
// serviceWorker.unregister();
1reaction
thomasmerycommented, Jul 3, 2020

Hi,

I’m sorry

I have ahed to switch to other matters and have had no time at all

I will have to come back to it but I don’t know when that will be

feel free to close if that’s convenient for you, I’ll re-open or make another issue when I can

thanks!

On Fri, Jul 3, 2020 at 10:06 AM Artem Zakharchenko notifications@github.com wrote:

Hey. Did you have any luck in debugging this issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mswjs/msw/issues/240#issuecomment-653413471, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALPETS3YUCJZ4JBRV25Z53RZWGSBANCNFSM4OEPAWLA .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress issues when using window.msw #1052 - GitHub
MSW is working as the app runs with a correctly mocked response. However, in my Cypress test I am trying to override the...
Read more >
MSW Runtime request handler not working in Cypress (NextJs ...
The MSW handlers in the worker setup (in NextJs) is intercepting the request from the Cypress test run without any issue.
Read more >
Cypress cy.intercept Problems - Gleb Bahmutov
Once the page is loaded, the application fetches the todo items, and everything is working as expected. The intercept works correctly. The bonus ......
Read more >
Easy mocking of API Requests in Cypress with Mock Service ...
The cy.intercept API is fantastic. However, it is an API that is specific to Cypress. When using MSW, we do lose some functionality...
Read more >
pactflow - Pact (Contract Testing)
See https://github.com/pactflow/example-consumer-cypress for an example ... which still doesn't appear to work with an arbitrary UUID for a version tag.
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