Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vitest): silence import analysis warning #6785

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -132,6 +132,7 @@ export function WorkspaceVitestPlugin(
allowClearScreen: false,
},
)
config.customLogger = silenceImportViteIgnoreWarning(config.customLogger)

return config
},
Expand Down
13 changes: 13 additions & 0 deletions packages/vitest/src/node/viteLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
}
2 changes: 1 addition & 1 deletion test/core/src/relative-import.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function dynamicRelativeImport(file: string) {
return import(`./${file}.ts`)
return import(/* @vite-ignore */ `./${file}.ts`)
}
2 changes: 1 addition & 1 deletion test/core/test/imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down