Open ytoshiki opened 2 weeks ago
Hi, @ytoshiki. Thanks for reporting this.
We've looked into the issue, and the setup and usage on your side look correct. There was a bug in MSW that prevented it from correctly forwarding the requests from the test frame to the respective worker instance (the one you create in tests).
Just upgrading msw to 2.5.0 in Vitest should fix the problem.
@sheremet-va also proposed moving the Vitest's worker to the test frame, which will also eliminate the problem by keeping all workers in a single context.
@kettanaito
I upgraded msw to 2.5.0, and it worked perfectly! I truly appreciate the effort and responsiveness from the team.
@sheremet-va
Thank you for addressing the bug. However, after upgrading to version 2.1.4, MSW is once again unable to intercept requests when using vi.mock
in tests.
The steps to reproduce are the same, and a small reproduction can be found here: https://github.com/ytoshiki/repro-msw-vitest-browser
I added an example to catch this regression in the future: https://github.com/vitest-tests/browser-examples/tree/main/examples/vue-msw-graphql
You can run it with
pnpm install
cd examples/vue-msw-graphql
pnpm test
Describe the bug
I am encountering an issue where MSW is unable to intercept GraphQL queries when using
vi.mock
from Vitest in my test setup. Regardless of which module is being mocked usingvi.mock
, MSW cannot intercept the requests whenvi.mock
is active.Reproduction
A minimal reproduction of the issue can be found here. https://github.com/ytoshiki/repro-msw-vitest-browser
Steps To Reproduce
npm install
to install dependencies.npm run test:browser
to start the test in the browser.components/index.test.ts
, MSW fails to mock the GraphQL queries. However, when pressing therefetch pokemon
button in the browser, the mock works as expected.vi.mock
section in the test file, kill the running task in the terminal, and rerunnpm run test:browser
vi.mock
.System Info
Used Package Manager
npm
Validations