Skip to content

Browser Mode: "Mock ... wasn't registered" error under load with mixed mocked/unmocked dependencies #7912

@Darksoulsong

Description

@Darksoulsong

Describe the bug

When running Vitest in browser mode (--browser) with a moderate-to-large number of test files, an error Error: Mock /node_modules/.vite/deps/<dependency>.js wasn't registered. occurs intermittently or reliably.

This issue arises specifically when:

  1. Some test files in the suite use vi.mock() to mock a specific dependency (e.g., react-router).
  2. Other test files import and use the same dependency without mocking it.
  3. The test suite size is large enough to likely induce concurrency/load (e.g., 100+ test files). With only a few test files or targeting single files, the error does not occur.

The error message points to Vite's pre-bundled dependency cache (.vite/deps/) and appears in the test files that were not attempting to use the mocked version. This suggests a potential race condition or breakdown in mock state isolation or Vite's dependency cache management within the browser runner when handling concurrent requests for both mocked and original versions of a module under load.
Common workarounds like adding the dependency to test.deps.inline or optimizeDeps.exclude did not resolve the issue in the minimal reproduction.

By the way, I do not currently intend to submit a PR for this issue.

Reproduction

A minimal reproduction is available at the following GitHub repository:
[Vitest Browser Mode Repro]

(I attempted to create a StackBlitz repro, but for some reason, that did not work, got the same issue mentioned here: #6911)

Steps to Reproduce:

  • Clone the repository: git clone git@github.com:Darksoulsong/vitest-browser-repro.git
  • Navigate to the project directory: cd vitest-browser-repro
  • Install dependencies: npm install
  • (Ensure Correct State): The repository should be in the state where test files (specifically RouterMock.spec.tsx and UnmockedRouterUser.spec.tsx) have been duplicated many times (e.g., 100+ copies) within src/components/__tests__/.
  • Run Vitest in browser mode: npm run test:browser (or vitest run --browser.headless)
  • Observe the output: Several test files (specifically the copies of UnmockedRouterUser.spec.tsx) will fail with the error Error: Mock /node_modules/.vite/deps/react-router.js wasn't registered..

System Info

System:
    OS: macOS 14.6.1
    CPU: (11) arm64 Apple M3 Pro
    Memory: 63.63 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.17.0 - ~/.nvm/versions/node/v20.17.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v20.17.0/bin/npm
  Browsers:
    Brave Browser: 135.1.77.101
    Chrome: 135.0.7049.115
    Safari: 17.6
  npmPackages:
    @vitejs/plugin-react: ^4.3.4 => 4.4.1 
    @vitejs/plugin-react-swc: ^3.9.0 => 3.9.0 
    @vitest/browser: ^3.1.2 => 3.1.2 
    playwright: ^1.52.0 => 1.52.0 
    vite: 5.4.11 => 5.4.11 
    vitest: 3.1.2 => 3.1.2

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions