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

MSW fails to intercept GraphQL queries when using vi.mock in Browser Mode #6766

Open
6 tasks done
ytoshiki opened this issue Oct 22, 2024 · 4 comments · Fixed by #6772
Open
6 tasks done

MSW fails to intercept GraphQL queries when using vi.mock in Browser Mode #6766

ytoshiki opened this issue Oct 22, 2024 · 4 comments · Fixed by #6772
Assignees
Labels
feat: browser Issues and PRs related to the browser runner p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@ytoshiki
Copy link

ytoshiki commented Oct 22, 2024

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 using vi.mock, MSW cannot intercept the requests when vi.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

  1. Run npm install to install dependencies.
  2. Run npm run test:browser to start the test in the browser.
  3. In components/index.test.ts, MSW fails to mock the GraphQL queries. However, when pressing the refetch pokemon button in the browser, the mock works as expected.
  4. Comment out the vi.mock section in the test file, kill the running task in the terminal, and rerun npm run test:browser
  vi.mock("anyModuleToMock", () => ({
    default: {
      key: "value",
    },
  }));
  1. Now, MSW successfully mocks the GraphQL queries without the vi.mock.

System Info

System:
    OS: macOS 14.5
    CPU: (8) arm64 Apple M2
    Memory: 70.97 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.12.2 - ~/.nodebrew/current/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 10.5.0 - ~/.nodebrew/current/bin/npm
    Watchman: 2024.04.15.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 130.0.6723.58
    Safari: 17.5
  npmPackages:
    @vitest/browser: ^2.1.3 => 2.1.3
    vitest: ^2.1.3 => 2.1.3

Used Package Manager

npm

Validations

@ytoshiki ytoshiki changed the title MSW fails to intercept GraphQL queries when using vi.mock MSW fails to intercept GraphQL queries when using vi.mock in Browser Mode Oct 22, 2024
@sheremet-va sheremet-va added feat: browser Issues and PRs related to the browser runner p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Oct 22, 2024
@kettanaito
Copy link
Contributor

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.

@ytoshiki
Copy link
Author

@kettanaito

I upgraded msw to 2.5.0, and it worked perfectly!
I truly appreciate the effort and responsiveness from the team.

@ytoshiki
Copy link
Author

@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

@sheremet-va sheremet-va reopened this Oct 29, 2024
@sheremet-va sheremet-va self-assigned this Oct 29, 2024
@sheremet-va
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: browser Issues and PRs related to the browser runner p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants