Skip to content

Commit 253625b

Browse files
hi-ogawaclaude
andcommitted
fix: batch consecutive same-action mocks in parallel
Resolve consecutive mock/unmock operations of the same type in parallel using Promise.all, only going sequential when the action type changes. This preserves correctness while avoiding unnecessary performance impact. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 120e9a7 commit 253625b

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

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

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

155-
for (const mock of BareModuleMocker.pendingIds) {
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-
)
155+
// batch consecutive mocks of the same action type to resolve in parallel,
156+
// but process different action types sequentially to preserve ordering
157+
let i = 0
158+
while (i < BareModuleMocker.pendingIds.length) {
159+
const action = BareModuleMocker.pendingIds[i].action
160+
const batch: typeof BareModuleMocker.pendingIds = []
161+
while (i < BareModuleMocker.pendingIds.length && BareModuleMocker.pendingIds[i].action === action) {
162+
batch.push(BareModuleMocker.pendingIds[i])
163+
i++
172164
}
165+
await Promise.all(
166+
batch.map(async (mock) => {
167+
const { id, url, external } = await this.resolveId(
168+
mock.id,
169+
mock.importer,
170+
)
171+
if (mock.action === 'unmock') {
172+
this.unmockPath(id)
173+
}
174+
if (mock.action === 'mock') {
175+
this.mockPath(
176+
mock.id,
177+
id,
178+
url,
179+
external,
180+
mock.type,
181+
mock.factory,
182+
)
183+
}
184+
}),
185+
)
173186
}
174187

175188
BareModuleMocker.pendingIds = []

0 commit comments

Comments
 (0)