From d7b5056ead972ef8a0a277294f74f0b64f94cd44 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Fri, 26 Jan 2024 11:38:45 +0100 Subject: [PATCH 1/2] fix(vitest): don't throw an error if mocked file was already imported --- packages/vitest/src/runtime/mocker.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/vitest/src/runtime/mocker.ts b/packages/vitest/src/runtime/mocker.ts index 8864ae97fb3c..ccd95e0d5337 100644 --- a/packages/vitest/src/runtime/mocker.ts +++ b/packages/vitest/src/runtime/mocker.ts @@ -168,7 +168,7 @@ export class VitestMocker { if (mock.type === 'unmock') this.unmockPath(fsPath) if (mock.type === 'mock') - this.mockPath(mock.id, fsPath, external, mock.factory, mock.throwIfCached) + this.mockPath(mock.id, fsPath, external, mock.factory) })) VitestMocker.pendingIds = [] @@ -408,19 +408,19 @@ export class VitestMocker { this.deleteCachedItem(id) } - public mockPath(originalId: string, path: string, external: string | null, factory: MockFactory | undefined, throwIfExists: boolean) { + public mockPath(originalId: string, path: string, external: string | null, factory: MockFactory | undefined) { const id = this.normalizePath(path) - const { config } = this.executor.state - const isIsolatedThreads = config.pool === 'threads' && (config.poolOptions?.threads?.isolate ?? true) - const isIsolatedForks = config.pool === 'forks' && (config.poolOptions?.forks?.isolate ?? true) + // const { config } = this.executor.state + // const isIsolatedThreads = config.pool === 'threads' && (config.poolOptions?.threads?.isolate ?? true) + // const isIsolatedForks = config.pool === 'forks' && (config.poolOptions?.forks?.isolate ?? true) // TODO: find a good way to throw this error even in non-isolated mode - if (throwIfExists && (isIsolatedThreads || isIsolatedForks || config.pool === 'vmThreads')) { - const cached = this.moduleCache.has(id) && this.moduleCache.getByModuleId(id) - if (cached && cached.importers.size) - 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`) - } + // if (throwIfExists && (isIsolatedThreads || isIsolatedForks || config.pool === 'vmThreads')) { + // const cached = this.moduleCache.has(id) && this.moduleCache.getByModuleId(id) + // if (cached && cached.importers.size) + // 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`) + // } const suitefile = this.getSuiteFilepath() const mocks = this.mockMap.get(suitefile) || {} From e5ca722e312a845d4e4e242aeb3d0403693be1d9 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Fri, 26 Jan 2024 11:54:51 +0100 Subject: [PATCH 2/2] chore: cleanup --- packages/vitest/src/runtime/mocker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/runtime/mocker.ts b/packages/vitest/src/runtime/mocker.ts index ccd95e0d5337..2073925d0240 100644 --- a/packages/vitest/src/runtime/mocker.ts +++ b/packages/vitest/src/runtime/mocker.ts @@ -1,6 +1,6 @@ import { existsSync, readdirSync } from 'node:fs' import vm from 'node:vm' -import { basename, dirname, extname, isAbsolute, join, relative, resolve } from 'pathe' +import { basename, dirname, extname, isAbsolute, join, resolve } from 'pathe' import { getType, highlight } from '@vitest/utils' import { isNodeBuiltin } from 'vite-node/utils' import { distDir } from '../paths'