-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcoverage-test-cases.ts
277 lines (232 loc) · 7.61 KB
/
coverage-test-cases.ts
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/// <reference types="node" />
import * as fs from 'fs';
import * as path from 'path';
import { expect } from 'chai';
import { describe, it } from 'mocha';
import puppeteer, {
Browser,
HTTPResponse,
JSCoverageEntry,
launch as launchPuppeteer,
Page,
} from 'puppeteer';
import { TestCase } from '../helpers/get-test-cases';
import { Interaction, performInteractions } from '../helpers/perform-interactions';
import { expectedCoverage, threshold } from './coverage-config';
import { getTestCases } from './helpers/get-coverage-test-cases';
const dummyContent = fs.readFileSync(
path.join(__dirname, 'helpers', 'test-page-dummy.html'),
{ encoding: 'utf-8' }
);
function generatePageContent(
standaloneBundlePath: string,
testCaseCode: string
): string {
return dummyContent
.replace('PATH_TO_STANDALONE_MODULE', standaloneBundlePath)
.replace('TEST_CASE_SCRIPT', testCaseCode);
}
const testStandalonePathEnvKey = 'TEST_STANDALONE_PATH';
const testStandalonePath: string = process.env[testStandalonePathEnvKey] || '';
interface InternalWindow {
interactions: Interaction[];
finishedSetup: Promise<() => void>;
afterInteractions: () => void;
}
function rmRf(dir: string): void {
if (!fs.existsSync(dir)) {
return;
}
fs.readdirSync(dir).forEach((file: string) => {
const filePath = path.join(dir, file);
if (fs.lstatSync(filePath).isDirectory()) {
rmRf(filePath);
} else {
fs.unlinkSync(filePath);
}
});
fs.rmdirSync(dir);
}
function generateAndSaveCoverageFile(coveredJs: string): void {
// Create output directory
const outDir = path.resolve(process.env.CMP_OUT_DIR || path.join(__dirname, '.gendata'));
rmRf(outDir);
fs.mkdirSync(outDir, { recursive: true });
try {
const filePath = path.join(outDir, 'covered.js');
fs.writeFileSync(filePath, coveredJs);
console.info('\nGenerated `covered.js` file for the coverage test.\n');
console.info(filePath);
} catch (error: unknown) {
console.warn('Unable to save `covered.js` file for the coverage test.');
console.error(error);
}
}
async function getCoverageResult(page: Page): Promise<JSCoverageEntry | null> {
const coverageEntries = await page.coverage.stopJSCoverage();
const getFileNameFromUrl = (url: string): string => url.split('/').at(-1) ?? '';
for (const entry of coverageEntries) {
const fileName = getFileNameFromUrl(entry.url);
if (fileName === 'test.js') { return entry; }
}
return null;
}
type CoverageTestResults = Record<string, JSCoverageEntry>;
interface Range {
start: number;
end: number;
}
interface CoverageResult {
usedBytes: number;
totalBytes: number;
coverageFile: string;
}
function mergeRanges(ranges: Range[]): Range[] {
ranges.sort((a: Range, b: Range) => {
if (a.start === b.start) {
return a.end - b.end;
}
return a.start - b.start;
});
const merged: Range[] = [];
for (const range of ranges) {
const last = merged.at(-1);
if (!last || last.end < range.start) {
merged.push(range);
} else {
last.end = Math.max(last.end, range.end);
}
}
return merged;
}
function consolidateCoverageResults(testResults: CoverageTestResults): CoverageResult {
const coveredRanges: Range[] = [];
let testScriptCode = '';
for (const [, coverageResult] of Object.entries(testResults)) {
coveredRanges.push(...coverageResult.ranges);
if (!testScriptCode) {
// Every test is against the same library file,
// therefore this will be the same for each coverageResult
testScriptCode = coverageResult.text;
}
}
const totalBytes = testScriptCode.length;
const mergedRanges = mergeRanges(coveredRanges);
let usedBytes = 0;
let coverageFile = '';
for (const range of mergedRanges) {
usedBytes += (range.end - range.start);
coverageFile += testScriptCode.slice(range.start, range.end) + '\n';
}
return {
usedBytes,
totalBytes,
coverageFile,
};
}
describe('Coverage tests', (): void => {
const puppeteerOptions: Parameters<typeof launchPuppeteer>[0] = {};
if (process.env.NO_SANDBOX) {
puppeteerOptions.args = ['--no-sandbox', '--disable-setuid-sandbox'];
}
let browser: Browser;
before(async () => {
expect(
testStandalonePath,
`path to test standalone module must be passed via ${testStandalonePathEnvKey} env var`
).to.have.length.greaterThan(0);
// note that we cannot use launchPuppeteer here as soon it wrong typing in puppeteer
// see https://github.com/puppeteer/puppeteer/issues/7529
const browserPromise = puppeteer.launch(puppeteerOptions);
browser = await browserPromise;
});
let testCaseCount = 0;
const coverageResults: CoverageTestResults = {};
const runTestCase = (testCase: TestCase) => {
testCaseCount += 1;
it(testCase.name, async () => {
const pageContent = generatePageContent(
testStandalonePath,
testCase.caseContent
);
const page = await browser.newPage();
await page.coverage.startJSCoverage();
await page.setViewport({ width: 600, height: 600 });
const errors: string[] = [];
page.on('pageerror', (error: Error) => {
errors.push(error.message);
});
page.on('response', (response: HTTPResponse) => {
if (!response.ok()) {
errors.push(
`Network error: ${response.url()} status=${response.status()}`
);
}
});
await page.setContent(pageContent, { waitUntil: 'load' });
await page.evaluate(() => {
return (window as unknown as InternalWindow).finishedSetup;
});
const interactionsToPerform = await page.evaluate(() => {
return (window as unknown as InternalWindow).interactions;
});
await performInteractions(page, interactionsToPerform);
await page.evaluate(() => {
return new Promise<void>((resolve: () => void) => {
(window as unknown as InternalWindow).afterInteractions();
window.requestAnimationFrame(() => {
setTimeout(resolve, 50);
});
});
});
if (errors.length !== 0) {
throw new Error(`Page has errors:\n${errors.join('\n')}`);
}
expect(errors.length).to.be.equal(
0,
'There should not be any errors thrown within the test page.'
);
const result = await getCoverageResult(page);
expect(result).not.to.be.equal(null);
if (result) {
coverageResults[testCase.name] = result;
}
});
};
const testCaseGroups = getTestCases();
for (const groupName of Object.keys(testCaseGroups)) {
if (groupName.length === 0) {
for (const testCase of testCaseGroups[groupName]) {
runTestCase(testCase);
}
} else {
describe(groupName, () => {
for (const testCase of testCaseGroups[groupName]) {
runTestCase(testCase);
}
});
}
}
it('number of test cases', () => {
// we need to have at least 1 test to check it
expect(testCaseCount).to.be.greaterThan(
0,
'there should be at least 1 test case'
);
});
after(async () => {
await browser.close();
const consolidatedResult = consolidateCoverageResults(coverageResults);
expect(consolidatedResult.usedBytes).to.be.lessThanOrEqual(consolidatedResult.totalBytes, 'Used bytes should be less than or equal to Total bytes.');
expect(consolidatedResult.usedBytes).to.be.greaterThan(0, 'Used bytes should be more than zero.');
if (process.env.GENERATE_COVERAGE_FILE === 'true') {
generateAndSaveCoverageFile(consolidatedResult.coverageFile);
}
const currentCoverage = parseFloat(((consolidatedResult.usedBytes / consolidatedResult.totalBytes) * 100).toFixed(2));
expect(currentCoverage).to.be.closeTo(expectedCoverage, threshold, `Please either update config to pass the test or improve coverage`);
console.log(`Current coverage is ${currentCoverage.toFixed(2)}% (${formatChange(currentCoverage - expectedCoverage)}%)`);
});
});
function formatChange(change: number): string {
return change < 0 ? change.toFixed(1) : `+${change.toFixed(1)}`;
}