-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathcoverage-builder.js
More file actions
52 lines (48 loc) · 1.78 KB
/
Copy pathcoverage-builder.js
File metadata and controls
52 lines (48 loc) · 1.78 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
import fs from 'fs';
import path from 'path';
import EC from 'eight-colors';
/**
* Create a coverage config from threshold information.
*
* @param params.bytes minimum number of bytes to pass
* @param params.statements minimum number of statements to pass
* @param params.branches minimum number of branches to pass
* @param params.functions minimum number of functions to pass
* @param params.lines minimum number of lines to pass
* @return config for coverage
*/
const coverageBuilder = (thresholds) => {
const allDirs = ['src', 'lib', 'build/src', 'ts_build/src', 'ecdsa', 'ecies'];
const dirs = allDirs.filter(dir => fs.existsSync(dir));
return {
reports: ['console-summary', 'v8', 'v8-json'],
outputDir: 'coverage',
entryFilter: (entry) => {
const fileName = entry.url.slice('file://'.length);
for (const dir of dirs) {
if (fileName.startsWith(path.join(process.cwd(), dir))
&& fileName.endsWith('.js')) return true;
}
return false;
},
onEnd: (coverageResults) => {
const errors = [];
const { summary } = coverageResults;
for (const k of Object.keys(thresholds)) {
const pct = summary[k].pct;
if (pct < thresholds[k]) {
errors.push(`${k[0].toUpperCase() + k.slice(1)} ${pct}% less than ${thresholds[k]}%`);
}
}
console.log(`See ${process.cwd()}/coverage/index.html for details on each file`);
console.log('Thresholds: ' + Object.keys(thresholds).map(key => `${key[0].toUpperCase() + key.slice(1)}: ${thresholds[key]}%`).join(' '));
if (errors.length) {
const errMsg = errors.join('\n');
console.log(EC.red(errMsg));
process.exit(1);
}
console.log(EC.green('Sufficient Coverage'));
}
};
};
export default coverageBuilder;