Skip to content

Commit 09ed1a6

Browse files
committed
test(@angular/build): add e2e for larger project with Vitest coverage
Adds an end-to-end test to verify Vitest coverage collection and threshold enforcement for a larger number of generated components, services, and pipes. This test ensures that all generated source files are correctly included in the coverage report in both JSDOM and browser environments. (cherry picked from commit 720a4ce)
1 parent f05ffd1 commit 09ed1a6

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { ng } from '../../utils/process';
2+
import { applyVitestBuilder } from '../../utils/vitest';
3+
import assert from 'node:assert';
4+
import { installPackage } from '../../utils/packages';
5+
import { exec } from '../../utils/process';
6+
import { updateJsonFile } from '../../utils/project';
7+
import { readFile } from '../../utils/fs';
8+
9+
export default async function () {
10+
await applyVitestBuilder();
11+
await installPackage('@vitest/coverage-v8@4');
12+
13+
// Add coverage and threshold configuration to ensure coverage is calculated.
14+
// Use the 'json' reporter to get a machine-readable output for assertions.
15+
await updateJsonFile('angular.json', (json) => {
16+
const project = Object.values(json['projects'])[0] as any;
17+
const test = project['architect']['test'];
18+
test.options = {
19+
coverageReporters: ['json', 'text'],
20+
coverageThresholds: {
21+
// The generated component/service/pipe files are basic
22+
// A threshold of 75 should be safe.
23+
statements: 75,
24+
},
25+
};
26+
});
27+
28+
const artifactCount = 100;
29+
const initialTestCount = 1;
30+
const generatedFiles: string[] = [];
31+
32+
// Generate a mix of components, services, and pipes
33+
for (let i = 0; i < artifactCount; i++) {
34+
const type = i % 3;
35+
const name = `test-artifact${i}`;
36+
let generateType;
37+
let fileSuffix;
38+
39+
switch (type) {
40+
case 0:
41+
generateType = 'component';
42+
fileSuffix = '.ts';
43+
break;
44+
case 1:
45+
generateType = 'service';
46+
fileSuffix = '.ts';
47+
break;
48+
default:
49+
generateType = 'pipe';
50+
fileSuffix = '-pipe.ts';
51+
break;
52+
}
53+
54+
await ng('generate', generateType, name, '--skip-tests=false');
55+
generatedFiles.push(`${name}${fileSuffix}`);
56+
}
57+
58+
const totalTests = initialTestCount + artifactCount;
59+
const expectedMessage = new RegExp(`${totalTests} passed`);
60+
const coverageJsonPath = 'coverage/test-project/coverage-final.json';
61+
62+
// Run tests in default (JSDOM) mode with coverage
63+
const { stdout: jsdomStdout } = await ng('test', '--no-watch', '--coverage');
64+
assert.match(jsdomStdout, expectedMessage, `Expected ${totalTests} tests to pass in JSDOM mode.`);
65+
66+
// Assert that every generated file is in the coverage report by reading the JSON output.
67+
const jsdomSummary = JSON.parse(await readFile(coverageJsonPath));
68+
const jsdomSummaryKeys = Object.keys(jsdomSummary);
69+
for (const file of generatedFiles) {
70+
const found = jsdomSummaryKeys.some((key) => key.endsWith(file));
71+
assert.ok(found, `Expected ${file} to be in the JSDOM coverage report.`);
72+
}
73+
74+
// Setup for browser mode
75+
await installPackage('playwright@1');
76+
await installPackage('@vitest/browser-playwright@4');
77+
await exec('npx', 'playwright', 'install', 'chromium', '--only-shell');
78+
79+
// Run tests in browser mode with coverage
80+
const { stdout: browserStdout } = await ng(
81+
'test',
82+
'--no-watch',
83+
'--coverage',
84+
'--browsers',
85+
'ChromiumHeadless',
86+
);
87+
assert.match(
88+
browserStdout,
89+
expectedMessage,
90+
`Expected ${totalTests} tests to pass in browser mode.`,
91+
);
92+
93+
// Assert that every generated file is in the coverage report for browser mode.
94+
const browserSummary = JSON.parse(await readFile(coverageJsonPath));
95+
const browserSummaryKeys = Object.keys(browserSummary);
96+
for (const file of generatedFiles) {
97+
const found = browserSummaryKeys.some((key) => key.endsWith(file));
98+
assert.ok(found, `Expected ${file} to be in the browser coverage report.`);
99+
}
100+
}

0 commit comments

Comments
 (0)