Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser: worker not mocking requests when vi.mock present #2398

Open
4 tasks done
charlieforward9 opened this issue Dec 20, 2024 · 10 comments
Open
4 tasks done

Browser: worker not mocking requests when vi.mock present #2398

charlieforward9 opened this issue Dec 20, 2024 · 10 comments
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser

Comments

@charlieforward9
Copy link

charlieforward9 commented Dec 20, 2024

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Browsers

Chromium (Chrome, Brave, etc.)

Reproduction repository

https://github.com/Agriculture-Intelligence/msw-vitest-browser-issue

Reproduction steps

  • Install aws-amplify, vitest, msw
  • Configure vitest and msw to support browser-based testing/mocking
  • Try mocking a call to the downloadData command from aws-amplify/storage module
  • Notice the non-detailed NETWORK ERR

Current behavior

Functional in development and node/jsdom testing, when configuration is changed to browser-based, fails.

Expected behavior

I expect the browser based mocking to work exactly the same as node based testing.

@charlieforward9 charlieforward9 added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser labels Dec 20, 2024
@charlieforward9 charlieforward9 changed the title ERR_NETWORK: Network Error with Amplify's downloadData, only in browser mode. ERR_NETWORK: Network Error in browser mode. Dec 20, 2024
@charlieforward9
Copy link
Author

charlieforward9 commented Dec 20, 2024

Looking at more of my tests, it seems like the real problem is that my handlers are not properly intercepting/mocking the network calls.

It appears to fire the request and match correctly, but not mock properly and end up somehow bypassing?

Am I configuring the worker incorrectly?

Please take a look at the sample repo so I can fix it and contribute it to an msw/example browser mode setup. Thank you

@charlieforward9
Copy link
Author

charlieforward9 commented Dec 20, 2024

I am also getting this at some places in my console when I run my tests...

 ... Warning: intercepted a request without a matching request handler:

  • GET /node_modules/...

i did not have any issues with any of the mocking when I was using the setupServer config, but now with setupWorker, there are a bunch of miscellaneous issues relating to external calls, including calls to http://localhost:63315/node_modules/...

@bdpartridge
Copy link

I'm hitting a seemingly similar issue while trying to use msw with vitest browser mode. My request handlers are being called, but ultimately msw is bypassing all requests, so all my mocked requests are resulting in 404 responses.

@bdpartridge
Copy link

I'm hitting a seemingly similar issue while trying to use msw with vitest browser mode. My request handlers are being called, but ultimately msw is bypassing all requests, so all my mocked requests are resulting in 404 responses.

I just realized that vitest browser is setting up its own msw request handler when vi.mock(...) is used to mock modules. Somehow this is interfering with my own request handlers, so it looks like this might be an issue with vitest browser and not msw.

@charlieforward9
Copy link
Author

vitest browser is setting up its own msw request handler when vi.mock(...) is used to mock modules. Somehow this is interfering with my own request handlers

@bdpartridge is it? Take a look at my reproduction. I don't use vi.mock and still get issues.

@bdpartridge
Copy link

@charlieforward9 You're right. It looks like yours is a different issue. I tried out your reproduction and noticed that the beforeAll hook in the vitest.setup.ts file wasn't awaiting the worker to start. After making this change:

beforeAll(async () => {
  Amplify.configure(amplifyconfig);
  await worker.start();
});

…I see the mocked 200 response for POST https://cognito-identity.us-east-2.amazonaws.com/, but there seems to be a separate CORS issue related to the preflight request to https://mock-staging.s3.us-east-2.amazonaws.com/path/to/file.json.

Anyway, sorry for adding noise to this thread. Good luck resolving this.

@charlieforward9

This comment was marked as outdated.

@charlieforward9

This comment was marked as outdated.

@charlieforward9 charlieforward9 changed the title ERR_NETWORK: Network Error in browser mode. ERR_NETWORK: Vague Network Error in browser mode. Jan 3, 2025
@charlieforward9 charlieforward9 changed the title ERR_NETWORK: Vague Network Error in browser mode. Browser: worker not mocking requests when vi.mock present in beforeAll Jan 9, 2025
@charlieforward9 charlieforward9 changed the title Browser: worker not mocking requests when vi.mock present in beforeAll Browser: worker not mocking requests when vi.mock present Jan 9, 2025
@charlieforward9
Copy link
Author

charlieforward9 commented Jan 9, 2025

@bdpartridge seems to be right

I just realized that vitest browser is setting up its own msw request handler when vi.mock(...) is used to mock modules. Somehow this is interfering with my own request handlers, so it looks like this might be an issue with vitest browser and not msw.

The reason it wasnt working on my repro without vi.mock calls was because I was originally returning undefined in the handler, leading to it bypassing the call.

If you think this isnt a problem with msw, have you opened an issue with vitest yet? Any workarounds other than commenting out vi.mock?

@e7h4n
Copy link

e7h4n commented Jan 19, 2025

I also encountered this issue, here is the minimal reproduction I tried: https://github.com/e7h4n/msw-vitest-failure/blob/main/index.test.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser
Projects
None yet
Development

No branches or pull requests

3 participants