Skip to content

Commit 631857d

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.
1 parent 960c80c commit 631857d

File tree

1 file changed

+103
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)