Skip to content

Commit d056eaa

Browse files
authored
Merge pull request #229 from bp-cos/feature/build-process
feat(build-process-updates). Introduced thresholds, pre-push and volta to the angular project
2 parents 01b056e + 7429c02 commit d056eaa

File tree

10 files changed

+146
-3
lines changed

10 files changed

+146
-3
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const filePath = path.join(__dirname, 'counter.txt');
5+
6+
try {
7+
// Read current value
8+
let current = 0;
9+
if (fs.existsSync(filePath)) {
10+
const content = fs.readFileSync(filePath, 'utf8').trim();
11+
current = parseInt(content, 10) || 0;
12+
}
13+
14+
console.info(`\n\nTests ran: ${current}\n\n`);
15+
} catch (err) {
16+
console.error('Error updating counter:', err.message);
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const filePath = path.join(__dirname, 'counter.txt');
5+
6+
try {
7+
// Read current value
8+
let current = 0;
9+
if (fs.existsSync(filePath)) {
10+
const content = fs.readFileSync(filePath, 'utf8').trim();
11+
current = parseInt(content, 10) || 0;
12+
}
13+
14+
// Increment
15+
const updated = current + 1;
16+
17+
// Write back
18+
fs.writeFileSync(filePath, String(updated));
19+
console.info(`\n\nCounter updated: ${current}${updated}\n\n`);
20+
} catch (err) {
21+
console.error('Error updating counter:', err.message);
22+
}
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

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ yarn-error.log
3737
testem.log
3838
/typings
3939

40+
.github/counter/counter.txt
41+
4042
# System files
4143
.DS_Store
4244
Thumbs.db

.husky/pre-commit

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
npx lint-staged
1+
npx lint-staged || {
2+
printf "\n\nERROR: Linting issues were found in the committed files. Please address them before proceeding.\n\n\n\n"
3+
exit 1
4+
}

.husky/pre-push

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
npm run build
2+
3+
npm run test:coverage || {
4+
printf "\n\nERROR: Testing errors or coverage issues were found. Please address them before proceeding.\n\n\n\n"
5+
exit 1
6+
}
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module.exports = {
22
preset: 'jest-preset-angular',
33
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
4+
globalSetup: '<rootDir>/jest.global-setup.ts',
5+
collectCoverage: true,
6+
coverageReporters: ['json-summary', 'lcov', 'clover'],
47
moduleNameMapper: {
58
'^@osf/(.*)$': '<rootDir>/src/app/$1',
69
'^@core/(.*)$': '<rootDir>/src/app/core/$1',
@@ -30,6 +33,14 @@ module.exports = {
3033
'!src/app/**/public-api.ts',
3134
],
3235
extensionsToTreatAsEsm: ['.ts'],
36+
coverageThreshold: {
37+
global: {
38+
branches: 11.89,
39+
functions: 12.12,
40+
lines: 37.27,
41+
statements: 37.83,
42+
},
43+
},
3344
testPathIgnorePatterns: [
3445
'<rootDir>/src/app/features/registry/',
3546
'<rootDir>/src/app/features/project/',

jest.global-setup.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { execSync } from 'child_process';
2+
3+
export default async function globalSetup(): Promise<void> {
4+
try {
5+
execSync('node .github/counter/counter.test.increment.js', { stdio: 'inherit' });
6+
} catch (err) {
7+
console.error('Test counter failed to increment:', err);
8+
}
9+
}

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
"start": "ng serve",
77
"build": "ng build",
88
"watch": "ng build --watch --configuration development",
9-
"test": "jest",
9+
"test": "jest && npm run test:display",
1010
"test:watch": "jest --watch",
11-
"test:coverage": "jest --coverage",
11+
"test:coverage": "jest --coverage && npm run test:display",
12+
"test:check-coverage-thresholds": "node .github/scripts/check-coverage-thresholds.js",
13+
"test:counter": "node .github/counter/counter.test.increment.js",
14+
"test:display": "node .github/counter/counter.test.display.js",
15+
"ci": "npm run lint && jest --coverage",
1216
"lint": "ng lint",
1317
"lint:fix": "ng lint --fix",
1418
"format": "prettier --write .",
@@ -90,5 +94,9 @@
9094
"prettier --write",
9195
"eslint --fix"
9296
]
97+
},
98+
"volta": {
99+
"node": "22.13.1",
100+
"npm": "10.9.2"
93101
}
94102
}

0 commit comments

Comments
 (0)