From 62c14cba2daba620855b2aad00f3d8e3c421d814 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 3 May 2023 14:41:20 +0200 Subject: [PATCH] fix: don't print esm warning, if package name is not found (#3292) --- packages/vitest/src/node/error.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/vitest/src/node/error.ts b/packages/vitest/src/node/error.ts index 86a977f8a525..d1d38b0fc4da 100644 --- a/packages/vitest/src/node/error.ts +++ b/packages/vitest/src/node/error.ts @@ -150,7 +150,14 @@ function handleImportOutsideModuleError(stack: string, ctx: Vitest) { else name = name.split('/')[0] - ctx.logger.error(c.yellow( + if (name) + printModuleWarningForPackage(ctx.logger, path, name) + else + printModuleWarningForSourceCode(ctx.logger, path) +} + +function printModuleWarningForPackage(logger: Logger, path: string, name: string) { + logger.error(c.yellow( `Module ${path} seems to be an ES Module but shipped in a CommonJS package. ` + `You might want to create an issue to the package ${c.bold(`"${name}"`)} asking ` + 'them to ship the file in .mjs extension or add "type": "module" in their package.json.' @@ -170,6 +177,13 @@ function handleImportOutsideModuleError(stack: string, ctx: Vitest) { }\n`))) } +function printModuleWarningForSourceCode(logger: Logger, path: string) { + logger.error(c.yellow( + `Module ${path} seems to be an ES Module but shipped in a CommonJS package. ` ++ 'To fix this issue, change the file extension to .mjs or add "type": "module" in your package.json.', + )) +} + export function displayDiff(diff: string, console: Console) { console.error(diff) }