Skip to content

feat: add check total script #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
node bin/check-coverage to/main2.js
node bin/only-covered main1.js main2.js

- name: Check totals 🛡
run: node bin/check-total --min 90

- name: Semantic Release 🚀
uses: cycjimmy/semantic-release-action@v2
env:
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,15 @@ only-covered --from examples/exclude-files/coverage/coverage-final.json main.js
check-coverage --from examples/exclude-files/coverage/coverage-final.json main.js
```

## check-total

If you generate coverage report using reporter `json-summary`, you can check the total statements percentage

```shell
check-total
# with default options
check-total --from coverage/coverage-summary.json --min 80
```

[ci image]: https://github.com/bahmutov/check-code-coverage/workflows/ci/badge.svg?branch=master
[ci url]: https://github.com/bahmutov/check-code-coverage/actions
39 changes: 39 additions & 0 deletions bin/check-total.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node
// @ts-check
const { join, resolve } = require('path')
const arg = require('arg')

const args = arg({
'--from': String, // input json-summary filename, by default "coverage/coverage-summary.json"
'--min': Number
})

const minStatementPercentage = args['--min'] || 80
const fromFilename = args['--from'] || join('coverage', 'coverage-summary.json')
const coverageFilename = resolve(fromFilename)

const coverage = require(coverageFilename)
const total = coverage.total
if (!total) {
console.error('Could not find "total" object in %s', fromFilename)
process.exit(1)
}

// total should have objects for lines, statements, functions and branches
// each object should have total, covered, skipped and pct numbers
const statements = total.statements
if (!statements) {
console.error('Could not find statements in total %o', total)
process.exit(1)
}

if (statements.pct < minStatementPercentage) {
console.log('🚨 Statement coverage %d is below minimum %d%%', statements.pct, minStatementPercentage)
console.log('file %s', coverageFilename)
process.exit(1)
}

console.log(
'✅ Total statement coverage %d%% is >= minimum %d%%',
statements.pct, minStatementPercentage
)
4 changes: 4 additions & 0 deletions coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"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}}
,"/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}}
,"/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}}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"bin": {
"check-coverage": "bin/check-coverage.js",
"only-covered": "bin/only-covered.js"
"only-covered": "bin/only-covered.js",
"check-total": "bin/check-total.js"
},
"files": [
"bin"
Expand Down