-
Notifications
You must be signed in to change notification settings - Fork 6
/
coverage.js
92 lines (82 loc) · 2.66 KB
/
coverage.js
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
const path = require('path');
const fs = require('fs');
const process = require('process');
function getAllPathsForPackagesSummaries() {
const getDirectories = (source) =>
fs
.readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const packagesPath = path.join(process.cwd(), 'packages');
const packageNames = getDirectories(packagesPath);
const packagesSummaries = packageNames.reduce((summary, packageName) => {
return {
...summary,
[packageName]: path.join(
packagesPath,
packageName,
'coverage',
'coverage-summary.json'
),
};
}, {});
return { ...packagesSummaries };
}
function readSummaryPerPackageAndCreateJoinedSummaryReportWithTotal(
packagesSummaryPaths
) {
return Object.keys(packagesSummaryPaths).reduce(
(summary, packageName) => {
const reportPath = packagesSummaryPaths[packageName];
if (fs.existsSync(reportPath)) {
const report = JSON.parse(fs.readFileSync(reportPath, 'utf8'));
const { total } = summary;
Object.keys(report.total).forEach((key) => {
if (total[key]) {
total[key].total += report.total[key].total;
total[key].covered += report.total[key].covered;
total[key].skipped += report.total[key].skipped;
total[key].pct = Number(
((total[key].covered / total[key].total) * 100).toFixed(2)
);
} else {
total[key] = { ...report.total[key] };
}
});
return { ...summary, [packageName]: report.total, total };
}
return summary;
},
{ total: {} }
);
}
function createCoverageReportForVisualRepresentation(coverageReport) {
return Object.keys(coverageReport).reduce((report, packageName) => {
const { lines, statements, functions, branches } =
coverageReport[packageName];
if (!lines) {
}
return {
...report,
[packageName]: {
lines: lines?.pct,
statements: statements?.pct,
functions: functions?.pct,
branches: branches?.pct,
},
};
}, {});
}
// Execution Stages
// 1. Read all coverage-total.json files
const packagesSummaryPaths = getAllPathsForPackagesSummaries();
// 2. Generate consolidated report
const currCoverageReport =
readSummaryPerPackageAndCreateJoinedSummaryReportWithTotal(
packagesSummaryPaths
);
// 3. Reformat the report for visual representation
const coverageReportForVisualRepresentation =
createCoverageReportForVisualRepresentation(currCoverageReport);
// 4. Print the report
console.table(coverageReportForVisualRepresentation);