Skip to content

Commit

Permalink
Merge pull request #30088 from storybookjs/jeppe/improve-coverage-error
Browse files Browse the repository at this point in the history
Vitest: Improve error message on missing coverage package
  • Loading branch information
JReinhold authored Dec 17, 2024
2 parents 91d1b03 + d51634e commit ba24c93
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
9 changes: 1 addition & 8 deletions code/addons/test/src/node/test-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions code/addons/test/src/node/vitest-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 collect coverage\n`;
}

this.testManager.reportFatalError('Failed to init Vitest', e);
this.testManager.reportFatalError(message, e);
return;
}

await this.setupWatchers();
Expand Down

0 comments on commit ba24c93

Please sign in to comment.