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