From 39041ee55d30ab14cfddb6787cf6263d190e3e8e Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 28 Oct 2024 19:35:40 +0900 Subject: [PATCH] fix(vitest): silence import analysis warning (#6785) --- packages/vitest/src/node/plugins/index.ts | 3 ++- packages/vitest/src/node/plugins/workspace.ts | 3 ++- packages/vitest/src/node/viteLogger.ts | 13 +++++++++++++ test/core/src/relative-import.ts | 2 +- test/core/test/imports.test.ts | 2 +- 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 4f1a8a3ad822..5b687e27e421 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -11,7 +11,7 @@ import { configDefaults, coverageConfigDefaults } from '../../defaults' import { generateScopedClassName } from '../../integrations/css/css-modules' import { resolveApiServerConfig } from '../config/resolveConfig' import { Vitest } from '../core' -import { createViteLogger } from '../viteLogger' +import { createViteLogger, silenceImportViteIgnoreWarning } from '../viteLogger' import { CoverageTransform } from './coverageTransform' import { CSSEnablerPlugin } from './cssEnabler' import { MocksPlugins } from './mocks' @@ -140,6 +140,7 @@ export async function VitestPlugin( allowClearScreen: false, }, ) + config.customLogger = silenceImportViteIgnoreWarning(config.customLogger) // If "coverage.exclude" is not defined by user, add "test.include" to "coverage.exclude" automatically if (userConfig.coverage?.enabled && !userConfig.coverage.exclude && userConfig.include && config.test) { diff --git a/packages/vitest/src/node/plugins/workspace.ts b/packages/vitest/src/node/plugins/workspace.ts index 3d18b3807c6e..bdffdbe16117 100644 --- a/packages/vitest/src/node/plugins/workspace.ts +++ b/packages/vitest/src/node/plugins/workspace.ts @@ -6,7 +6,7 @@ import { deepMerge } from '@vitest/utils' import { basename, dirname, relative, resolve } from 'pathe' import { configDefaults } from '../../defaults' import { generateScopedClassName } from '../../integrations/css/css-modules' -import { createViteLogger } from '../viteLogger' +import { createViteLogger, silenceImportViteIgnoreWarning } from '../viteLogger' import { CoverageTransform } from './coverageTransform' import { CSSEnablerPlugin } from './cssEnabler' import { MocksPlugins } from './mocks' @@ -132,6 +132,7 @@ export function WorkspaceVitestPlugin( allowClearScreen: false, }, ) + config.customLogger = silenceImportViteIgnoreWarning(config.customLogger) return config }, diff --git a/packages/vitest/src/node/viteLogger.ts b/packages/vitest/src/node/viteLogger.ts index 0a9fb79da1e9..110db4b31d96 100644 --- a/packages/vitest/src/node/viteLogger.ts +++ b/packages/vitest/src/node/viteLogger.ts @@ -135,3 +135,16 @@ export function createViteLogger( return logger } + +// silence warning by Vite for statically not analysizable dynamimc import +export function silenceImportViteIgnoreWarning(logger: Logger): Logger { + return { + ...logger, + warn(msg, options) { + if (msg.includes('The above dynamic import cannot be analyzed by Vite')) { + return + } + logger.warn(msg, options) + }, + } +} diff --git a/test/core/src/relative-import.ts b/test/core/src/relative-import.ts index 53c7740d37c3..042013f62c8a 100644 --- a/test/core/src/relative-import.ts +++ b/test/core/src/relative-import.ts @@ -1,3 +1,3 @@ export function dynamicRelativeImport(file: string) { - return import(`./${file}.ts`) + return import(/* @vite-ignore */ `./${file}.ts`) } diff --git a/test/core/test/imports.test.ts b/test/core/test/imports.test.ts index a6d2970cec73..c333ad31e8ee 100644 --- a/test/core/test/imports.test.ts +++ b/test/core/test/imports.test.ts @@ -90,7 +90,7 @@ test('dynamic import throws an error', async () => { test('can import @vite/client', async () => { const name = '@vite/client' await expect(import(name)).resolves.not.toThrow() - await expect(import(`/${name}`)).resolves.not.toThrow() + await expect(import(/* @vite-ignore */ `/${name}`)).resolves.not.toThrow() }) describe('importing special files from node_modules', async () => {