Skip to content

Commit

Permalink
fix: fix mock race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 12, 2024
1 parent 4e60333 commit 2ce84d0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
4 changes: 1 addition & 3 deletions packages/vitest/src/runtime/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,7 @@ export class VitestExecutor extends ViteNodeRunner {
}

async resolveUrl(id: string, importer?: string) {
if (VitestMocker.pendingIds.length) {
await this.mocker.resolveMocks()
}
await this.mocker.resolveMocks()

if (importer && importer.startsWith('mock:')) {
importer = importer.slice(5)
Expand Down
14 changes: 8 additions & 6 deletions packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface MockContext {

export class VitestMocker {
static pendingIds: PendingSuiteMock[] = []
static pendingResolve?: Promise<unknown>
private spyModule?: typeof import('@vitest/spy')
private primitives: {
Object: typeof Object
Expand Down Expand Up @@ -159,12 +160,14 @@ export class VitestMocker {
}

public async resolveMocks() {
if (!VitestMocker.pendingIds.length) {
await VitestMocker.pendingResolve
const pendingIds = VitestMocker.pendingIds
if (pendingIds.length === 0) {
return
}

await Promise.all(
VitestMocker.pendingIds.map(async (mock) => {
VitestMocker.pendingIds = []
VitestMocker.pendingResolve = Promise.all(
pendingIds.map(async (mock) => {
const { fsPath, external } = await this.resolvePath(
mock.id,
mock.importer,
Expand All @@ -183,8 +186,7 @@ export class VitestMocker {
}
}),
)

VitestMocker.pendingIds = []
await VitestMocker.pendingResolve
}

private async callFunctionMock(dep: string, mock: ManualMockedModule) {
Expand Down
1 change: 1 addition & 0 deletions test/core/test/mocking/race-condition-mocked.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'original'
22 changes: 22 additions & 0 deletions test/core/test/mocking/race-condition.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect, test, vi } from 'vitest'

vi.mock('./race-condition-mocked', () => ({ default: 'mocked' }))

test('import in parallel', async () => {
const all = await Promise.all([
import('./race-condition-mocked').then(v => v.default),
import('./race-condition-mocked').then(v => v.default),
import('./race-condition-mocked').then(v => v.default),
import('./race-condition-mocked').then(v => v.default),
import('./race-condition-mocked').then(v => v.default),
])
expect(all).toMatchInlineSnapshot(`
[
"mocked",
"mocked",
"mocked",
"mocked",
"mocked",
]
`)
})

0 comments on commit 2ce84d0

Please sign in to comment.