forked from mrsone40/vets-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-coverage-report.js
More file actions
executable file
·127 lines (112 loc) · 3.88 KB
/
app-coverage-report.js
File metadata and controls
executable file
·127 lines (112 loc) · 3.88 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const Table = require('cli-table');
const { getAppManifests } = require('../config/manifest-helpers');
const printCoverage = coverageResults => {
// Create a new table with headers
const coverageTable = new Table({
head: [
'Application (src/applications)',
'Lines',
'Functions',
'Statements',
'Branches',
],
});
// Add each app coverage result to the table
Object.values(coverageResults).forEach(cov => {
const appLocation =
cov.path.substr(0, cov.path.lastIndexOf('/')) || 'All Files';
coverageTable.push({
[appLocation]: [
`${cov.lines.pct}%`,
`${cov.functions.pct}%`,
`${cov.statements.pct}%`,
`${cov.branches.pct}%`,
],
});
});
console.log(coverageTable.toString());
};
const generateAppCoverage = (rootDir, coverageSummary) => {
// Get application manifests & entry names
const manifests = getAppManifests(path.join(__dirname, '../'));
// Set initial coverage object
const initialCoverage = Object.assign(
...manifests.map(({ entryName, entryFile }) => ({
[entryName]: {
path: entryFile.replace(rootDir, ''),
lines: { total: 0, covered: 0, pct: 0 },
statements: { total: 0, covered: 0, pct: 0 },
functions: { total: 0, covered: 0, pct: 0 },
branches: { total: 0, covered: 0, pct: 0 },
},
})),
);
// Iterate through coverages that are under src/applications
return Object.keys(coverageSummary)
.filter(fullPath => fullPath.includes('src/applications'))
.reduce((acc, fullCoveragePath) => {
// Find coverage if it exists by checking if the path of the coverage result file
// includes an application path, e.g. 'vre/chapter36/'
const coverageItem = Object.values(acc).find(appCov =>
fullCoveragePath.includes(
`${appCov.path.substring(0, appCov.path.lastIndexOf('/'))}/`,
),
);
// Average coverageResult with respective coverageItem segment if present
// Each 'seg' represents coverage by line, function, statement, and branch
Object.keys(coverageItem || {}).forEach(seg => {
if (seg !== 'path') {
coverageItem[seg].total +=
coverageSummary[fullCoveragePath][seg].total;
coverageItem[seg].covered +=
coverageSummary[fullCoveragePath][seg].covered;
coverageItem[seg].pct = (
(coverageItem[seg].covered / coverageItem[seg].total) * 100 || 0
).toFixed(2);
}
});
return acc;
}, initialCoverage);
};
const generateCoverage = (rootDir, coverageSummary) => {
const appCoverage = generateAppCoverage(rootDir, coverageSummary);
return {
'all-files': {
path: '/',
...coverageSummary.total,
},
...appCoverage,
};
};
const logCoverage = coverageResults => {
// convert coverageResults JSON object to prettified string
const data = JSON.stringify(coverageResults, null, 4);
// write coverageResults string to file
const outputFile = path.join(
__dirname,
'../coverage/test-coverage-report.json',
);
fs.writeFile(outputFile, data, err => {
if (err) {
throw err;
}
console.log('JSON data is saved.');
});
};
// Root directory of application folders
const applicationDir = path.join(__dirname, '../src/applications/');
// Check if coverage-summary.json exists before generating coverage
if (fs.existsSync(path.join(__dirname, '../coverage/coverage-summary.json'))) {
const coverageSummaryJson = JSON.parse(
fs.readFileSync(path.join(__dirname, '../coverage/coverage-summary.json')),
);
// Generate and print coverage
const appCoverages = generateCoverage(applicationDir, coverageSummaryJson);
logCoverage(appCoverages);
printCoverage(appCoverages);
} else {
console.log('./coverage/coverage-summary.json not found.');
}