Skip to content

Commit 8f6d88f

Browse files
authored
feat: add check total script (#3)
``` check-total # with default options check-total --from coverage/coverage-summary.json --min 80 ```
1 parent 7233255 commit 8f6d88f

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
node bin/check-coverage to/main2.js
1717
node bin/only-covered main1.js main2.js
1818
19+
- name: Check totals 🛡
20+
run: node bin/check-total --min 90
21+
1922
- name: Semantic Release 🚀
2023
uses: cycjimmy/semantic-release-action@v2
2124
env:

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,15 @@ only-covered --from examples/exclude-files/coverage/coverage-final.json main.js
2020
check-coverage --from examples/exclude-files/coverage/coverage-final.json main.js
2121
```
2222

23+
## check-total
24+
25+
If you generate coverage report using reporter `json-summary`, you can check the total statements percentage
26+
27+
```shell
28+
check-total
29+
# with default options
30+
check-total --from coverage/coverage-summary.json --min 80
31+
```
32+
2333
[ci image]: https://github.com/bahmutov/check-code-coverage/workflows/ci/badge.svg?branch=master
2434
[ci url]: https://github.com/bahmutov/check-code-coverage/actions

bin/check-total.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env node
2+
// @ts-check
3+
const { join, resolve } = require('path')
4+
const arg = require('arg')
5+
6+
const args = arg({
7+
'--from': String, // input json-summary filename, by default "coverage/coverage-summary.json"
8+
'--min': Number
9+
})
10+
11+
const minStatementPercentage = args['--min'] || 80
12+
const fromFilename = args['--from'] || join('coverage', 'coverage-summary.json')
13+
const coverageFilename = resolve(fromFilename)
14+
15+
const coverage = require(coverageFilename)
16+
const total = coverage.total
17+
if (!total) {
18+
console.error('Could not find "total" object in %s', fromFilename)
19+
process.exit(1)
20+
}
21+
22+
// total should have objects for lines, statements, functions and branches
23+
// each object should have total, covered, skipped and pct numbers
24+
const statements = total.statements
25+
if (!statements) {
26+
console.error('Could not find statements in total %o', total)
27+
process.exit(1)
28+
}
29+
30+
if (statements.pct < minStatementPercentage) {
31+
console.log('🚨 Statement coverage %d is below minimum %d%%', statements.pct, minStatementPercentage)
32+
console.log('file %s', coverageFilename)
33+
process.exit(1)
34+
}
35+
36+
console.log(
37+
'✅ Total statement coverage %d%% is >= minimum %d%%',
38+
statements.pct, minStatementPercentage
39+
)

coverage/coverage-summary.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{"total": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
2+
,"/Users/gleb/git/instrument-example/src/App.js": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
3+
,"/Users/gleb/git/instrument-example/src/index.js": {"lines":{"total":2,"covered":2,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":2,"covered":2,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
4+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"bin": {
77
"check-coverage": "bin/check-coverage.js",
8-
"only-covered": "bin/only-covered.js"
8+
"only-covered": "bin/only-covered.js",
9+
"check-total": "bin/check-total.js"
910
},
1011
"files": [
1112
"bin"

0 commit comments

Comments
 (0)