From e64e65cc85e41cbf9a01b6b10cdea613b1a0b884 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Tue, 17 Dec 2024 14:14:35 +0100 Subject: [PATCH 1/2] improve error message on missing coverage package --- code/addons/test/src/node/test-manager.ts | 9 +-------- code/addons/test/src/node/vitest-manager.ts | 14 ++++++++++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/code/addons/test/src/node/test-manager.ts b/code/addons/test/src/node/test-manager.ts index 4770f2b5a174..1595c2da3fa9 100644 --- a/code/addons/test/src/node/test-manager.ts +++ b/code/addons/test/src/node/test-manager.ts @@ -53,14 +53,7 @@ export class TestManager { coverage: this.coverage, }); } catch (e) { - const isV8 = e.message?.includes('@vitest/coverage-v8'); - const isIstanbul = e.message?.includes('@vitest/coverage-istanbul'); - - if (e.message?.includes('Error: Failed to load url') && (isIstanbul || isV8)) { - const coveragePackage = isIstanbul ? 'coverage-istanbul' : 'coverage-v8'; - e.message = `Please install the @vitest/${coveragePackage} package to run with coverage`; - } - this.reportFatalError('Failed to change coverage mode', e); + this.reportFatalError('Failed to change coverage configuration', e); } } } diff --git a/code/addons/test/src/node/vitest-manager.ts b/code/addons/test/src/node/vitest-manager.ts index d55d3bc6d5b1..120927a86036 100644 --- a/code/addons/test/src/node/vitest-manager.ts +++ b/code/addons/test/src/node/vitest-manager.ts @@ -89,15 +89,21 @@ export class VitestManager { try { await this.vitest.init(); } catch (e) { + let message = 'Failed to initialize Vitest'; const isV8 = e.message?.includes('@vitest/coverage-v8'); const isIstanbul = e.message?.includes('@vitest/coverage-istanbul'); - if (e.message?.includes('Error: Failed to load url') && (isIstanbul || isV8)) { + if ( + (e.message?.includes('Failed to load url') && (isIstanbul || isV8)) || + // Vitest will sometimes not throw the correct missing-package-detection error, so we have to check for this as well + (e instanceof TypeError && + e?.message === "Cannot read properties of undefined (reading 'name')") + ) { const coveragePackage = isIstanbul ? 'coverage-istanbul' : 'coverage-v8'; - e.message = `Please install the @vitest/${coveragePackage} package to run with coverage`; + message += `\n\nPlease install the @vitest/${coveragePackage} package to run with coverage\n`; } - - this.testManager.reportFatalError('Failed to init Vitest', e); + this.testManager.reportFatalError(message, e); + return; } await this.setupWatchers(); From dcd5549d42656926e69a2af353f742f0833a7132 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Tue, 17 Dec 2024 20:30:25 +0100 Subject: [PATCH 2/2] Update code/addons/test/src/node/vitest-manager.ts --- code/addons/test/src/node/vitest-manager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/addons/test/src/node/vitest-manager.ts b/code/addons/test/src/node/vitest-manager.ts index 120927a86036..b872cd96ff6b 100644 --- a/code/addons/test/src/node/vitest-manager.ts +++ b/code/addons/test/src/node/vitest-manager.ts @@ -100,7 +100,7 @@ export class VitestManager { e?.message === "Cannot read properties of undefined (reading 'name')") ) { const coveragePackage = isIstanbul ? 'coverage-istanbul' : 'coverage-v8'; - message += `\n\nPlease install the @vitest/${coveragePackage} package to run with coverage\n`; + message += `\n\nPlease install the @vitest/${coveragePackage} package to collect coverage\n`; } this.testManager.reportFatalError(message, e); return;