How to properly add a Cypress e2e setup to a project using MSW? #2577
Unanswered
kleinfreund
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I know this topic has come up several times before:
But I'm still struggling with this and I'm trying to arrive at a clean approach that ideally doesn't involve altering application code or stuffing things on the
windowobject (if possible).At the core, my question is: How to properly add a Cypress e2e setup to a project using MSW?
I'm working on a Vue application using Vite (both facts are besides the point). In the main JS entry point, MSW is started behind an environment variable check:
main.js:mocks/browser.js:This setup works really well and MSW is great at what its doing. However, I'm really struggling adding a Cypress setup on top of this. I'm able to run a test just fine, but I'm not yet able to correctly provide a custom MSW request handler in it.
My first test looks like this:
And my Cypress support file looks like this:
This doesn't work: the test runs, but the app never uses the custom request handler, only the default one.
What happens in order is the following:
cy.visitcall)In a way, this even makes sense to me. After all, the app does start its own MSW worker with handlers and so it's plausible that it can't see the "other" worker's handlers or that it implicitly resets them (I don't know how that works internally: are there now two independent workers with their own sets of handlers?).
But then how can I have my Cypress test register new handlers that my app can use?
I want to avoid making changes to any application code and, if possible, also to stuff things on the
windowobject though as a last resort, it'd be acceptable.Any help is appreciated. I'm mostly looking for conceptual answers as I didn't provide a minimal sample project. I might simply be holding things wrong.
Notes:
Using cypress 14.5.4, msw 2.11.1, vite 7.1.4.
I tried starting the MSW worker in the Cypress setup, then using Cypress'
window:before:loadevent to set the promise returned byworker.startasmswPromiseon thewindowobject.The application can then avoid starting its own MSW worker and await the promise instead. However, it doesn't look like the application is seeing any of the default handlers in this case and network requests fail instead of being mocked.
In other words, it seems strictly necessary for the MSW worker to be started by the application and not the Cypress setup code. That seems more sensible anyway. But perhaps I'm still missing a detail here. I have not tried to shared the
workerinstance with the application because that seems to much of a breach of isolating the application from test code.I tried using the same
workerobject in the Cypress setup that the application code uses. There are still two places callingworker.startand so I reckon that's not much different than what I'm currently seeing.I'm investigating the use of Cypress, Playwright, and Playwright via Vitest Browser Mode at the same time.
With Playwright, I can't get mocking from within tests to work either. The problem seems more or less identical to the one outlined here. I've integrated https://github.com/mswjs/playwright, but the handlers added via the networking fixture have no effect (see Can't overwrite handlers from within test playwright#21).
Only with the Playwright via Vitest Browser Mode do I get a smooth integration with MSW going. The downside of that being that I can't access Playwright directly and so I can't modify Playwright's own
pageobject, for example.Beta Was this translation helpful? Give feedback.
All reactions