Browser Mode leaks a manual mock across spec files when the same module is mocked via two ids (original) (raw)

Describe the bug

This repro is pure TypeScript. Vue components are not required.

src/probe.spec.ts registers two manual mocks for the same logical module using two different raw ids:

src/target.spec.ts does not register any mock. It only imports another module that touches ~/modal again.

Both spec files pass when run individually, but when they run in the same Browser Mode process, the second file fails after the first file passes:

[vitest] There was an error when mocking a module
[birpc] rpc is closed, cannot call "resolveManualMock"

This looks like a Browser Mode mock lifecycle bug. During the second file import, Vitest appears to still try resolving the manual mock registered in the previous file, even though the second file did not register that mock.

Reproduction

Repository repro:

A-kirami/vitest-browser-manual-mock

Commands:

bun vitest run --config vite.config.ts --no-cache src/probe.spec.ts bun vitest run --config vite.config.ts --no-cache src/target.spec.ts bun vitest run --config vite.config.ts --no-cache --reporter verbose src/probe.spec.ts src/target.spec.ts

Notes:

Expected behavior:

Actual behavior:

Additional observation:

On a fresh run, vitest list also fails:

bun vitest list --config vite.config.ts --no-cache src/probe.spec.ts src/target.spec.ts

with:

Mock /src/modal.ts wasn't registered. This is probably a Vitest error. Please, open a new issue with reproduction.

System Info

System: OS: Windows 11 10.0.26200 CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H Memory: 6.99 GB / 31.63 GB Binaries: Node: 23.5.0 npm: 10.9.2 bun: 1.3.10 Deno: 2.1.4 Browsers: Edge: Chromium (140.0.3485.54) Internet Explorer: 11.0.26100.7309 npmPackages: @vitest/browser-playwright: 4.1.0 playwright:1.58.2 vite: 7.3.1 vitest: 4.1.0

Used Package Manager

bun

Validations