Skip to content

Commit f08ce4b

Browse files
fix: apply queued mocks from doMock() in queue order (fixes #10706) (#11127)
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
1 parent c4473e4 commit f08ce4b

2 files changed

Lines changed: 61 additions & 21 deletions

File tree

‎packages/vitest/src/runtime/moduleRunner/bareModuleMocker.ts‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,35 +152,38 @@ export class BareModuleMocker implements TestModuleMocker {
152152
return
153153
}
154154

155+
const pendingIds = BareModuleMocker.pendingIds
156+
BareModuleMocker.pendingIds = []
157+
155158
const resolveMock = async (mock: PendingSuiteMock) => {
156-
const { id, url, external } = await this.resolveId(
157-
mock.id,
158-
mock.importer,
159-
)
160-
if (mock.action === 'unmock') {
161-
this.unmockPath(id)
162-
}
163-
if (mock.action === 'mock') {
164-
this.mockPath(
165-
mock.id,
166-
id,
167-
url,
168-
external,
169-
mock.type,
170-
mock.factory,
171-
)
172-
}
159+
const resolved = await this.resolveId(mock.id, mock.importer)
160+
return { mock, ...resolved }
173161
}
174162

175163
// group consecutive mocks of the same action type together,
176164
// resolve in parallel inside each group, but run groups sequentially
177165
// to preserve mock/unmock ordering
178-
const groups = groupByConsecutiveAction(BareModuleMocker.pendingIds)
166+
const groups = groupByConsecutiveAction(pendingIds)
179167
for (const group of groups) {
180-
await Promise.all(group.map(resolveMock))
168+
// apply in queue order: two mocks of the same path can resolve out of
169+
// order, and the last queued one must win
170+
const resolvedGroup = await Promise.all(group.map(resolveMock))
171+
for (const { mock, id, url, external } of resolvedGroup) {
172+
if (mock.action === 'unmock') {
173+
this.unmockPath(id)
174+
}
175+
if (mock.action === 'mock') {
176+
this.mockPath(
177+
mock.id,
178+
id,
179+
url,
180+
external,
181+
mock.type,
182+
mock.factory,
183+
)
184+
}
185+
}
181186
}
182-
183-
BareModuleMocker.pendingIds = []
184187
}
185188

186189
// public method to avoid circular dependency

‎test/e2e/test/mocking.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,43 @@ ${importChecks}
363363
`)
364364
})
365365

366+
test('the last doMock of the same path wins', async () => {
367+
// repeats doMock twice for the same path without an import in between
368+
// vi.doMock('/mock-lib-0', () => ({ value: 'first' }));
369+
// vi.doMock('/mock-lib-0', () => ({ value: 'second' }));
370+
// ...
371+
// then, the last registered factory should be used
372+
// import('/mock-lib-0') // => { value: 'second' }
373+
const N = 20
374+
const mockEntries = Array.from({ length: N }, (_, i) => `\
375+
vi.doMock('/mock-lib-${i}', () => ({ value: 'first' }));
376+
vi.doMock('/mock-lib-${i}', () => ({ value: 'second' }));
377+
`).join('\n')
378+
const importChecks = Array.from({ length: N }, (_, i) => `\
379+
await expect(import('/mock-lib-${i}')).resolves.toEqual({ value: 'second' });
380+
`).join('\n')
381+
382+
const { stderr, errorTree } = await runInlineTests({
383+
'./basic.test.js': `
384+
import { test, expect, vi } from 'vitest'
385+
386+
test('duplicate mock of the same path (last one should win)', async () => {
387+
${mockEntries}
388+
${importChecks}
389+
})
390+
`,
391+
})
392+
393+
expect(stderr).toBe('')
394+
expect(errorTree()).toMatchInlineSnapshot(`
395+
{
396+
"basic.test.js": {
397+
"duplicate mock of the same path (last one should win)": "passed",
398+
},
399+
}
400+
`)
401+
})
402+
366403
test.for([
367404
'node',
368405
'playwright',

0 commit comments

Comments
 (0)