Skip to content

Commit 1c3888b

Browse files
saryn17sheremet-va
andauthored
fix(browser): check mock path boundaries (fix #11361) (#11362)
Co-authored-by: Ryosei Sato <[email protected]> Co-authored-by: Vladimir <[email protected]>
1 parent a029e76 commit 1c3888b

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎packages/mocker/src/browser/mocker.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { CreateMockInstanceProcedure } from '../automocker'
22
import type { MockedModule, MockedModuleType } from '../registry'
33
import type { ModuleMockContext, ModuleMockOptions, TestModuleMocker } from '../types'
44
import type { ModuleMockerInterceptor } from './interceptor'
5+
import { withTrailingSlash } from '@vitest/utils/helpers'
56
import { extname, join } from 'pathe'
67
import { mockObject } from '../automocker'
78
import { AutomockedModule, MockerRegistry, RedirectedModule } from '../registry'
@@ -277,11 +278,11 @@ export class ModuleMocker implements TestModuleMocker {
277278
const fsRoot = join('/@fs/', config.root)
278279

279280
// URL can be /file/path.js, but path is resolved to /file/path
280-
if (path.startsWith(config.root)) {
281+
if (path.startsWith(withTrailingSlash(config.root))) {
281282
return path.slice(config.root.length)
282283
}
283284

284-
if (path.startsWith(fsRoot)) {
285+
if (path.startsWith(withTrailingSlash(fsRoot))) {
285286
return path.slice(fsRoot.length)
286287
}
287288

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "project1-utils"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { test, expect, vi } from 'vitest';
22
import project2 from "../project2/index.js"
3+
import project1Utils from "../project1-utils/index.js"
34
import "./imported-test.js"
45
import "../project3/imported-test.js"
56

67
vi.mock("../project2/index.js", () => ({
78
default: 'project2-mocked'
89
}))
910

11+
vi.mock("../project1-utils/index.js", () => ({
12+
default: 'project1-utils-mocked'
13+
}))
14+
1015
test("basic", () => {
1116
expect(project2).toMatchInlineSnapshot(`"project2-mocked"`)
1217
})
18+
19+
test("sibling directory that shares the root prefix", () => {
20+
expect(project1Utils).toMatchInlineSnapshot(`"project1-utils-mocked"`)
21+
})

0 commit comments

Comments
 (0)