Skip to content

Commit 1e11c56

Browse files
committed
feat(check-coverage): Added a coverage check locally and during the build process
1 parent 0691138 commit 1e11c56

5 files changed

Lines changed: 69 additions & 1 deletion

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fs = require('fs');
2+
const { execSync } = require('child_process');
3+
const coverage = require('../../coverage/coverage-summary.json');
4+
const jestConfig = require('../../jest.config.js');
5+
6+
const summary = coverage.total;
7+
const thresholds = jestConfig.coverageThreshold.global;
8+
9+
let failed = false;
10+
const errors = [];
11+
for (const key of ['branches', 'functions', 'lines', 'statements']) {
12+
const current = summary[key].pct;
13+
const threshold = thresholds[key];
14+
if (current > threshold) {
15+
errors.push(
16+
`Coverage for ${key} (${current}%) is above the threshold (${threshold}%).\n\tPlease update the coverageThreshold.global.${key} in the jest.config.js to ${current}!`
17+
)
18+
failed = true;
19+
}
20+
}
21+
22+
if (failed) {
23+
execSync('clear', { stdio: 'inherit' });
24+
console.log('\n\nCongratulations! You have successfully run the coverage check and added tests.');
25+
console.log('\n\nThe jest.config.js file is not insync with your new test additions.');
26+
console.log('Please update the coverage thresholds in jest.config.js.');
27+
console.log('You will need to commit again once you have updated the jst.config.js file.');
28+
console.log('This is only necessary until we hit 100% coverage.\n\n');
29+
errors.forEach(err => console.error(`${err}\n`));
30+
process.exit(1);
31+
}

.github/workflows/test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Run Jest Tests
2+
3+
on:
4+
pull_request:
5+
branches: ['*'] # or change to match your default branch
6+
push:
7+
branches: ['*']
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
17+
- name: Use Node.js
18+
uses: actions/setup-node@v4
19+
with:
20+
node-version: 22.13.1
21+
22+
- name: Install dependencies
23+
run: npm ci
24+
25+
- name: Run Jest tests with coverage
26+
run: npm run ci
27+
28+
- name: Check coverage thresholds
29+
run: npm run test:check-coverage-thresholds

.husky/pre-push

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ npm run test:coverage || {
44
printf "\n\nERROR: Testing errors or coverage issues were found. Please address them before proceeding.\n\n\n\n"
55
exit 1
66
}
7+
8+
npm run test:check-coverage-thresholds || {
9+
printf "\n\nERROR: Coverage thresholds were not met. Please address them before proceeding.\n\n\n\n"
10+
exit 1
11+
}

jest.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module.exports = {
22
preset: 'jest-preset-angular',
33
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
4+
collectCoverage: true,
5+
coverageReporters: ['json-summary', 'lcov', 'clover'],
46
moduleNameMapper: {
57
'^@osf/(.*)$': '<rootDir>/src/app/$1',
68
'^@core/(.*)$': '<rootDir>/src/app/core/$1',
@@ -32,10 +34,10 @@ module.exports = {
3234
extensionsToTreatAsEsm: ['.ts'],
3335
coverageThreshold: {
3436
global: {
35-
statements: 37.83,
3637
branches: 11.89,
3738
functions: 12.12,
3839
lines: 37.27,
40+
statements: 37.83,
3941
},
4042
},
4143
testPathIgnorePatterns: [

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"test": "jest",
1010
"test:watch": "jest --watch",
1111
"test:coverage": "jest --coverage",
12+
"test:check-coverage-thresholds": "node .github/scripts/check-coverage-thresholds.js",
1213
"ci": "npm run lint && npm run test:coverage",
1314
"lint": "ng lint",
1415
"lint:fix": "ng lint --fix",

0 commit comments

Comments
 (0)