Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ export async function createVitestConfigPlugin(
(browser || testConfig?.browser?.enabled) &&
(options.coverage.enabled || testConfig?.coverage?.enabled)
) {
// Validate that enabled browsers support V8 coverage
if (browser?.instances) {
const unsupportedBrowsers = browser.instances.filter(
(instance) => !['chrome', 'chromium', 'edge'].includes(instance.browser),
);

if (unsupportedBrowsers.length > 0) {
throw new Error(
`Code coverage is enabled, but the following configured browsers do not support the V8 coverage provider: ` +
`${unsupportedBrowsers.map((i) => i.browser).join(', ')}. ` +
`V8 coverage is only supported on Chromium-based browsers (e.g., Chrome, Chromium, Edge). ` +
`Please disable coverage or remove the unsupported browsers.`,
);
}
}
Comment on lines +158 to +171
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current validation only checks the instances property of the CLI-provided browser options. It should also validate the name property (which Vitest uses if instances is not defined) and consider the browser configuration defined in the Vitest configuration file (testConfig.browser). Without this, users configuring browsers via vitest.config.ts or using the name property will bypass this validation, leading to the incomplete coverage data this PR aims to prevent.

Suggested change
if (browser?.instances) {
const unsupportedBrowsers = browser.instances.filter(
(instance) => !['chrome', 'chromium', 'edge'].includes(instance.browser),
);
if (unsupportedBrowsers.length > 0) {
throw new Error(
`Code coverage is enabled, but the following configured browsers do not support the V8 coverage provider: ` +
`${unsupportedBrowsers.map((i) => i.browser).join(', ')}. ` +
`V8 coverage is only supported on Chromium-based browsers (e.g., Chrome, Chromium, Edge). ` +
`Please disable coverage or remove the unsupported browsers.`,
);
}
}
const activeBrowser = browser ?? testConfig?.browser;
const unsupportedBrowsers = activeBrowser?.instances
? activeBrowser.instances
.filter((i) => !['chrome', 'chromium', 'edge'].includes(i.browser))
.map((i) => i.browser)
: activeBrowser?.name && !['chrome', 'chromium', 'edge'].includes(activeBrowser.name)
? [activeBrowser.name]
: [];
if (unsupportedBrowsers.length > 0) {
throw new Error(
'Code coverage is enabled, but the following configured browsers do not support the V8 coverage provider: ' +
unsupportedBrowsers.join(', ') + '. ' +
'V8 coverage is only supported on Chromium-based browsers (e.g., Chrome, Chromium, Edge). ' +
'Please disable coverage or remove the unsupported browsers.',
);
}


projectPlugins.unshift(createSourcemapSupportPlugin());
setupFiles.unshift('virtual:source-map-support');
}
Expand Down
31 changes: 31 additions & 0 deletions tests/e2e/tests/vitest/browser-coverage-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import assert from 'node:assert/strict';
import { applyVitestBuilder } from '../../utils/vitest';
import { execAndCaptureError } from '../../utils/process';
import { installPackage } from '../../utils/packages';
import { stripVTControlCharacters } from 'node:util';

export default async function (): Promise<void> {
await applyVitestBuilder();

// Install necessary packages to pass the provider check
await installPackage('playwright@1');
await installPackage('@vitest/browser-playwright@4');
await installPackage('@vitest/coverage-v8@4');

// Run test with coverage and an unsupported browser
const error = await execAndCaptureError('ng', [
'test',
'--no-watch',
'--coverage',
'--browsers',
'firefox',
]);
const output = stripVTControlCharacters(error.message);

// Verify that the expected error message is present
assert.match(
output,
/Code coverage is enabled, but the following configured browsers do not support the V8 coverage provider: firefox/,
'Expected validation error for unsupported browser with coverage.',
);
}
Loading