Skip to content

Commit c479d9c

Browse files
authoredDec 28, 2022
fix: don't hang when mocking module with circular dependency (#2572)
1 parent 9f41edd commit c479d9c

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import b from './export-default-circle-index'
2+
3+
export default function () {
4+
return b()
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import b from './export-default-circle-b'
2+
3+
export default function () {
4+
return b()
5+
}

‎examples/mocks/test/circular.spec.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import { expect, test, vi } from 'vitest'
22
import { main } from '../src/main.js'
3+
import x from '../src/export-default-circle-index'
34

45
vi.mock('../src/A', async () => ({
56
...(await vi.importActual<any>('../src/A')),
67
funcA: () => 'mockedA',
78
}))
89

9-
test('main', () => {
10+
vi.mock('../src/export-default-circle-b')
11+
12+
test('can import actual inside mock factory', () => {
1013
expect(main()).toBe('mockedA')
1114
})
15+
16+
test('can mock a circular dependency', () => {
17+
expect(x()).toBe(undefined)
18+
})

‎packages/vitest/src/runtime/mocker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,13 @@ export class VitestMocker {
359359

360360
if (mock === null) {
361361
const cache = this.moduleCache.get(mockPath)
362-
if (cache?.exports)
362+
if (cache.exports)
363363
return cache.exports
364364

365365
const exports = {}
366366
// Assign the empty exports object early to allow for cycles to work. The object will be filled by mockObject()
367367
this.moduleCache.set(mockPath, { exports })
368-
const mod = await this.runner.directRequest(url, url, [])
368+
const mod = await this.runner.directRequest(url, url, callstack)
369369
this.mockObject(mod, exports)
370370
return exports
371371
}

0 commit comments

Comments
 (0)
Please sign in to comment.