Describe the bug
With the Playwright provider and the root at packages/app, vi.mock('../../app-utils/src/value') has no effect. A mock for packages/lib works.
The module is served as /@fs/.../packages/app-utils/src/value.ts. resolveMockPath() strips the root with path.startsWith(fsRoot), which also matches app-utils. The mock ends up registered as -utils/src/value.ts, and the Playwright route never matches the request.
|
private resolveMockPath(path: string) { |
|
const config = this.config |
|
const fsRoot = join('/@fs/', config.root) |
|
|
|
// URL can be /file/path.js, but path is resolved to /file/path |
|
if (path.startsWith(config.root)) { |
|
return path.slice(config.root.length) |
|
} |
|
|
|
if (path.startsWith(fsRoot)) { |
|
return path.slice(fsRoot.length) |
|
} |
|
|
|
return path |
|
} |
packages/mocker/src/node/resolver.ts checks the boundary with withTrailingSlash(root). I have a fix with a test that does the same in resolveMockPath() and will open a PR.
I used Claude Code to help track this down.
Reproduction
https://github.com/sary8/vitest-browser-mock-sibling-root
npm install
npx playwright install chromium
npm test
The module in packages/app-utils test fails with expected 'real' to be 'mocked'.
System Info
System:
OS: Windows 11 10.0.26200
CPU: (16) x64 AMD Ryzen AI 7 350 w/ Radeon 860M
Memory: 4.19 GB / 31.29 GB
Binaries:
Node: 25.2.1 - C:\Program Files\nodejs\node.EXE
npm: 11.6.2 - C:\Program Files\nodejs\npm.CMD
pnpm: 10.33.0 - C:\Users\sarys\AppData\Roaming\npm\pnpm.CMD
bun: 1.4.2 - C:\Users\sarys\AppData\Roaming\npm\bun.CMD
Browsers:
Chrome: 154.0.8037.57
Edge: Chromium (153.0.4234.32)
npmPackages:
@vitest/browser-playwright: 5.0.2 => 5.0.2
playwright: 1.63.0 => 1.63.0
vitest: 5.0.2 => 5.0.2
Used Package Manager
npm
Validations
Describe the bug
With the Playwright provider and the root at
packages/app,vi.mock('../../app-utils/src/value')has no effect. A mock forpackages/libworks.The module is served as
/@fs/.../packages/app-utils/src/value.ts.resolveMockPath()strips the root withpath.startsWith(fsRoot), which also matchesapp-utils. The mock ends up registered as-utils/src/value.ts, and the Playwright route never matches the request.vitest/packages/mocker/src/browser/mocker.ts
Lines 258 to 272 in 428e2e5
packages/mocker/src/node/resolver.tschecks the boundary withwithTrailingSlash(root). I have a fix with a test that does the same inresolveMockPath()and will open a PR.I used Claude Code to help track this down.
Reproduction
https://github.com/sary8/vitest-browser-mock-sibling-root
npm install npx playwright install chromium npm testThe
module in packages/app-utilstest fails withexpected 'real' to be 'mocked'.System Info
System: OS: Windows 11 10.0.26200 CPU: (16) x64 AMD Ryzen AI 7 350 w/ Radeon 860M Memory: 4.19 GB / 31.29 GB Binaries: Node: 25.2.1 - C:\Program Files\nodejs\node.EXE npm: 11.6.2 - C:\Program Files\nodejs\npm.CMD pnpm: 10.33.0 - C:\Users\sarys\AppData\Roaming\npm\pnpm.CMD bun: 1.4.2 - C:\Users\sarys\AppData\Roaming\npm\bun.CMD Browsers: Chrome: 154.0.8037.57 Edge: Chromium (153.0.4234.32) npmPackages: @vitest/browser-playwright: 5.0.2 => 5.0.2 playwright: 1.63.0 => 1.63.0 vitest: 5.0.2 => 5.0.2Used Package Manager
npm
Validations