-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
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:
- Some test files in the suite use
vi.mock()to mock a specific dependency (e.g.,react-router). - Other test files import and use the same dependency without mocking it.
- 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.tsxandUnmockedRouterUser.spec.tsx) have been duplicated many times (e.g., 100+ copies) withinsrc/components/__tests__/. - Run Vitest in browser mode:
npm run test:browser(orvitest run --browser.headless) - Observe the output: Several test files (specifically the copies of
UnmockedRouterUser.spec.tsx) will fail with the errorError: 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.2Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.