Skip to content

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

@ytoshiki

Description

@ytoshiki

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

Metadata

Metadata

Assignees

Labels

feat: browserIssues and PRs related to the browser runnerp3-minor-bugAn edge case that only affects very specific usage (priority)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions