mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
16k stars 522 forks source link

When running Cypress tests in Electron, MSW activation occurs after test completion, causing tests to fail #2114

Closed jolientb closed 3 months ago

jolientb commented 8 months ago

Prerequisites

Environment check

Browsers

No response

Reproduction repository

https://github.com/jolientb/msw-cypress.git

Reproduction steps

Current behavior

Cypress test fails in Electron browser When you run the cypress e2e test in the Electron browser for the first time the test fails, because msw is only enabled after the test has finished. When running the test again in the same session, msw is enabled on time and the test does succeed.

msw-cypress

Ps. When running the test in the chrome browser or in Electron 118 (headless) msw is enabled on time, and the test succeeds. And I tried adding an await before worker.start(), but this doesn't solve the problem.

Expected behavior

Electron browser: MSW should be enabled before test is completed When you run the cypress e2e test in the Electron browser for the first time, msw should be enabled before the test finishes and the test should succeed (just like in the Chrome browser).

kettanaito commented 3 months ago

Hi, @jolientb.

Your MSW setup seems good to me. The root cause of your issue is that Cypress doesn't seem to wait for your application to render. You may want to explicitly instruct Cypress to wait until a certain HTML element is visible on the page before running the test suite.

This is not an MSW issue.