Skip to content

Commit 1574491

Browse files
committed
fix(@angular/build): validate V8 coverage support for browsers in Vitest
This change introduces a validation check in the Vitest runner to ensure that code coverage is only enabled when using supported Chromium-based browsers. Since the Angular CLI integration currently relies exclusively on the V8 coverage provider, running tests in non-Chromium browsers like Firefox or Safari with coverage enabled would result in incomplete data or missing reports. By adding this check, developers will receive a clear, actionable error message early in the process, preventing confusion and ensuring reliable coverage reports.
1 parent 0f2342c commit 1574491

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ export async function createVitestConfigPlugin(
154154
(browser || testConfig?.browser?.enabled) &&
155155
(options.coverage.enabled || testConfig?.coverage?.enabled)
156156
) {
157+
// Validate that enabled browsers support V8 coverage
158+
if (browser?.instances) {
159+
const unsupportedBrowsers = browser.instances.filter(
160+
(instance) => !['chrome', 'chromium', 'edge'].includes(instance.browser),
161+
);
162+
163+
if (unsupportedBrowsers.length > 0) {
164+
throw new Error(
165+
`Code coverage is enabled, but the following configured browsers do not support the V8 coverage provider: ` +
166+
`${unsupportedBrowsers.map((i) => i.browser).join(', ')}. ` +
167+
`V8 coverage is only supported on Chromium-based browsers (e.g., Chrome, Chromium, Edge). ` +
168+
`Please disable coverage or remove the unsupported browsers.`,
169+
);
170+
}
171+
}
172+
157173
projectPlugins.unshift(createSourcemapSupportPlugin());
158174
setupFiles.unshift('virtual:source-map-support');
159175
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import assert from 'node:assert/strict';
2+
import { applyVitestBuilder } from '../../utils/vitest';
3+
import { execAndCaptureError } from '../../utils/process';
4+
import { installPackage } from '../../utils/packages';
5+
import { stripVTControlCharacters } from 'node:util';
6+
7+
export default async function (): Promise<void> {
8+
await applyVitestBuilder();
9+
10+
// Install necessary packages to pass the provider check
11+
await installPackage('playwright@1');
12+
await installPackage('@vitest/browser-playwright@4');
13+
14+
// Run test with coverage and an unsupported browser
15+
const error = await execAndCaptureError('ng', [
16+
'test',
17+
'--no-watch',
18+
'--coverage',
19+
'--browsers',
20+
'firefox',
21+
]);
22+
const output = stripVTControlCharacters(error.message);
23+
24+
// Verify that the expected error message is present
25+
assert.match(
26+
output,
27+
/Code coverage is enabled, but the following configured browsers do not support the V8 coverage provider: firefox/,
28+
'Expected validation error for unsupported browser with coverage.',
29+
);
30+
}

0 commit comments

Comments
 (0)