Skip to content

Commit 7ed5df3

Browse files
committed
Add coverage comparison script
A script that takes two dirs containing coverage summaries for each app and package, measured at two points (i.e. PR vs main) and fails if reg- gression for any project is detected. This script needs to be merged before a PR is created with a workflow action referencing it, as that PR itself will trigger the job and fail if the script is not found.
1 parent bf5d385 commit 7ed5df3

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

scripts/compare-coverage.mjs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* compare-coverage.mjs
3+
*
4+
* Compares Vitest coverage summary files for each app and package
5+
* captured at two points, and fails if coverage has regressed.
6+
*
7+
* Intended for use in CI to prevent pull requests from decreasing test coverage.
8+
*
9+
* Arguments:
10+
* node compare-coverage.mjs <pr-dir> <baseline-dir>
11+
*
12+
* Exit codes:
13+
* 0 coverage is unchanged or improved
14+
* 1 coverage regressed in at least on project (or general script error)
15+
*/
16+
17+
import fs from "node:fs";
18+
19+
const prDir = process.argv[2];
20+
const baseDir = process.argv[3];
21+
22+
const projects = fs.readdirSync(prDir).filter((file) => file.endsWith(".json"));
23+
24+
let failed = false;
25+
26+
for (const file of projects) {
27+
const prPath = `${prDir}/${file}`;
28+
const basePath = `${baseDir}/${file}`;
29+
30+
if (!fs.existsSync(basePath)) {
31+
console.log(`⚠️ ${file.replace(".json", "")}: no baseline found, skipping`);
32+
continue;
33+
}
34+
35+
const pr = JSON.parse(fs.readFileSync(prPath));
36+
const base = JSON.parse(fs.readFileSync(basePath));
37+
38+
const prLines = pr.total.lines.pct;
39+
const baseLines = base.total.lines.pct;
40+
41+
const delta = (prLines - baseLines).toFixed(2);
42+
const name = file.replace(".json", "");
43+
44+
console.log(`\nProject: ${name}`);
45+
console.log(`Base: ${baseLines}%`);
46+
console.log(`PR: ${prLines}%`);
47+
console.log(`Δ: ${delta}%`);
48+
49+
if (prLines < baseLines) {
50+
console.log("❌ Regression detected");
51+
failed = true;
52+
} else {
53+
console.log("✅ OK");
54+
}
55+
}
56+
57+
if (failed) {
58+
process.exit(1);
59+
}

0 commit comments

Comments
 (0)