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 fails to find the correct client in iframe scenarios (like with Cypress)

See original GitHub issue

Describe the bug

This is a tricky one. I suppose the root error is failing to find the correct client that emitted a specific event, which then throws an error in ./mockServiceWorker.js#117 on if (activeClientIds.has(client.id)) because client is undefined.

It only happens in Cypress tests, which changes how the app is rendered I suppose, and has to do with iframes I guess.

More info in screenshots.

Environment

  • msw: 0.27.1
  • nodejs: 14.13.0
  • npm: 7.6.1
  • cypress: 6.6.0

msw@0.26.2 does not have this issue

Chrome: 89.0.4389.82

but also happens on Electron and Firefox, doesn’t seem to be browser related

To Reproduce

Steps to reproduce the behavior:

  1. Run msw on app start
  2. Run Cypress to test app (or another way to put app into an iframe?)
  3. Re-run tests until error (usually first or second is enough)
  4. See error

I realise this is bad, please take a look at the screenshots, perhaps it will become clearer how to reproduce

Expected behavior

msw can be used in Cypress/iframes without issues.

Screenshots

It starts by intercepting this request, I don’t know why because it’s definitely not a path I mock. I suppose it should simply pass through.

image

It then finds the client, but it’s nested so on to trying to find the parent I suppose?

image

This is where it goes wrong. allClients has no id that matches one in activeClientIds.

image

Therefore client is now undefined here, and client.id throws.

image

I hope that helps, I’m sure it has to do with iframes but I don’t know why the ids don’t match.

Curiously, the first Cypress test always passes (ids match and all), but the second on always fail.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
tget4commented, Mar 12, 2021

I’m having the exact same problem. The client id for the incoming event doesn’t match any of the active clients. I’m running a nextjs server and it blows up on any request to /__cypress/ or /_next/.

Updating line 117 on mockServiceWorker.js to this seems to resolve the problem

  if (client && activeClientIds.has(client.id)) {
2reactions
Wimpertcommented, Mar 13, 2021

The fix looks good, but we need to get a test so this doesn’t happen again. I’m trying to emulate an iframe reload scenario in Playwright, and it doesn’t seem to be working as we need.

Please, can anybody share some context when exactly this happens? (test suite change, page reload, navigation, etc.) Any details will be helpful to finding the root cause and writing a test for that. Thanks.

In my case it happens when I hit the rerun button in Cypress.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Mock Service Worker (MSW) | by Daniel Afonso
Are you tired of maintaining a dedicated mock server? Do you find that mocking fetch or axios causes more problems in your tests...
Read more >
Cypress Tips and Tricks | Better world by better software
Whenever a test fails, I watch the failure video side by side with the video from the last successful test run.
Read more >
How to Polyfill node core modules in webpack 5
When I did that I got the error: Module not found: Error: You attempted to import /Users/username/abja/identity/node_modules/crypto-browserify/ ...
Read more >
Untitled
Kirkby college holidays, Dr. perry spine institute of nevada, Partially soluble substances examples, Best clean funny videos, Bela crkva serbia wiki, ...
Read more >
Codebase Conversion: Migrating a MEAN AngularJS app to ...
Since then, we've used it for a variety of other DB updates, like ... When a Next page is loaded into an iframe...
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