-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcheck-coverage-thresholds.js
More file actions
58 lines (52 loc) · 2.24 KB
/
check-coverage-thresholds.js
File metadata and controls
58 lines (52 loc) · 2.24 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
const coverage = require('../../coverage/coverage-summary.json');
const jestConfig = require('../../jest.config.ts');
const summary = coverage.total;
const thresholds = jestConfig.coverageThreshold.global;
let failed = false;
const errors = [];
const leftBracket = `*${' '.repeat(3)}`;
const rightBracket = `${' '.repeat(3)}*`;
const warnMessage =
'This is only a warn. In the future, you will not be able to push to github until the thresholds are updated.';
function formatErrorsWithAlignedStars(error, isSecondline = false) {
const totalWidth = warnMessage.length;
const spaces = totalWidth - error.length - (isSecondline ? 4 : 0);
return `${leftBracket}${isSecondline ? '\t' : ''}${error}${' '.repeat(spaces)}${rightBracket}`;
}
// Example usage:
for (const key of ['branches', 'functions', 'lines', 'statements']) {
const current = summary[key].pct;
const threshold = thresholds[key];
if (current > threshold) {
errors.push(
formatErrorsWithAlignedStars(`Coverage for ${key} (${current}%) is above the threshold (${threshold}%).`)
);
errors.push(
formatErrorsWithAlignedStars(
`Please update the coverageThreshold.global.${key} in the jest.config.ts to ---> ${current} <---`,
true
)
);
errors.push(`${leftBracket}${' '.repeat(warnMessage.length)}${rightBracket}`);
failed = true;
}
}
if (failed) {
const stars = '*'.repeat(warnMessage.length + 8);
console.log('\n\nCongratulations! You have successfully run the coverage check and added tests.');
console.log('\n\nThe jest.config.ts file is not insync with your new test additions.');
console.log('Please update the coverage thresholds in jest.config.ts.');
console.log('You will need to commit again once you have updated the jst.config.ts file.');
console.log('This is only necessary until we hit 100% coverage.');
console.log(`\n\n${stars}`);
errors.forEach((err) => {
console.error(err);
});
console.log(`${stars}`);
console.log(`\n\n${stars}`);
console.log(`${leftBracket}${' '.repeat(warnMessage.length)}${rightBracket}`);
console.log(`${leftBracket}${warnMessage}${rightBracket}`);
console.log(`${leftBracket}${' '.repeat(warnMessage.length)}${rightBracket}`);
console.log(`${stars}\n\n`);
// process.exit(1);
}