Skip to content

Commit

Permalink
fix(vite-node): circular import stuck (#3418)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored May 24, 2023
1 parent e6792a9 commit 632eef4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions examples/mocks/src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { funcA } from './A'
import { funcB } from './B'

export function main() {
return funcA()
}

export function mainB() {
return funcB()
}
11 changes: 10 additions & 1 deletion examples/mocks/test/circular.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { expect, test, vi } from 'vitest'
import { main } from '../src/main.js'
import { main, mainB } from '../src/main.js'
import x from '../src/export-default-circle-index'

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

vi.mock('../src/B', async () => ({
...(await vi.importActual<any>('../src/B')),
funcB: () => 'mockedB',
}))

vi.mock('../src/export-default-circle-b')

test('can import actual inside mock factory', () => {
expect(main()).toBe('mockedA')
})

test('can import in top level and inside mock factory', () => {
expect(mainB()).toBe('mockedB')
})

test('can mock a circular dependency', () => {
expect(x()).toBe(undefined)
})
2 changes: 1 addition & 1 deletion packages/vite-node/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class ViteNodeRunner {
const getStack = () => `stack:\n${[...callstack, fsPath].reverse().map(p => ` - ${p}`).join('\n')}`

// check circular dependency
if (callstack.includes(fsPath) || callstack.some(c => this.moduleCache.get(c).importers?.has(fsPath))) {
if (callstack.includes(fsPath) || callstack.some(c => this.moduleCache.get(c).importers?.has(fsPath)) || mod.importers.has(importee)) {
if (mod.exports)
return mod.exports
}
Expand Down

0 comments on commit 632eef4

Please sign in to comment.