Skip to content

Commit fff1a27

Browse files
authoredJan 26, 2024
fix(vitest): don't throw an error if mocked file was already imported (#5050)
1 parent 6dae3fe commit fff1a27

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed
 

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { existsSync, readdirSync } from 'node:fs'
22
import vm from 'node:vm'
3-
import { basename, dirname, extname, isAbsolute, join, relative, resolve } from 'pathe'
3+
import { basename, dirname, extname, isAbsolute, join, resolve } from 'pathe'
44
import { getType, highlight } from '@vitest/utils'
55
import { isNodeBuiltin } from 'vite-node/utils'
66
import { distDir } from '../paths'
@@ -168,7 +168,7 @@ export class VitestMocker {
168168
if (mock.type === 'unmock')
169169
this.unmockPath(fsPath)
170170
if (mock.type === 'mock')
171-
this.mockPath(mock.id, fsPath, external, mock.factory, mock.throwIfCached)
171+
this.mockPath(mock.id, fsPath, external, mock.factory)
172172
}))
173173

174174
VitestMocker.pendingIds = []
@@ -408,19 +408,19 @@ export class VitestMocker {
408408
this.deleteCachedItem(id)
409409
}
410410

411-
public mockPath(originalId: string, path: string, external: string | null, factory: MockFactory | undefined, throwIfExists: boolean) {
411+
public mockPath(originalId: string, path: string, external: string | null, factory: MockFactory | undefined) {
412412
const id = this.normalizePath(path)
413413

414-
const { config } = this.executor.state
415-
const isIsolatedThreads = config.pool === 'threads' && (config.poolOptions?.threads?.isolate ?? true)
416-
const isIsolatedForks = config.pool === 'forks' && (config.poolOptions?.forks?.isolate ?? true)
414+
// const { config } = this.executor.state
415+
// const isIsolatedThreads = config.pool === 'threads' && (config.poolOptions?.threads?.isolate ?? true)
416+
// const isIsolatedForks = config.pool === 'forks' && (config.poolOptions?.forks?.isolate ?? true)
417417

418418
// TODO: find a good way to throw this error even in non-isolated mode
419-
if (throwIfExists && (isIsolatedThreads || isIsolatedForks || config.pool === 'vmThreads')) {
420-
const cached = this.moduleCache.has(id) && this.moduleCache.getByModuleId(id)
421-
if (cached && cached.importers.size)
422-
throw new Error(`[vitest] Cannot mock "${originalId}" because it is already loaded by "${[...cached.importers.values()].map(i => relative(this.root, i)).join('", "')}".\n\nPlease, remove the import if you want static imports to be mocked, or clear module cache by calling "vi.resetModules()" before mocking if you are going to import the file again. See: https://vitest.dev/guide/common-errors.html#cannot-mock-mocked-file-js-because-it-is-already-loaded`)
423-
}
419+
// if (throwIfExists && (isIsolatedThreads || isIsolatedForks || config.pool === 'vmThreads')) {
420+
// const cached = this.moduleCache.has(id) && this.moduleCache.getByModuleId(id)
421+
// if (cached && cached.importers.size)
422+
// throw new Error(`[vitest] Cannot mock "${originalId}" because it is already loaded by "${[...cached.importers.values()].map(i => relative(this.root, i)).join('", "')}".\n\nPlease, remove the import if you want static imports to be mocked, or clear module cache by calling "vi.resetModules()" before mocking if you are going to import the file again. See: https://vitest.dev/guide/common-errors.html#cannot-mock-mocked-file-js-because-it-is-already-loaded`)
423+
// }
424424

425425
const suitefile = this.getSuiteFilepath()
426426
const mocks = this.mockMap.get(suitefile) || {}

0 commit comments

Comments
 (0)
Please sign in to comment.