Skip to content

Commit 0f08bb2

Browse files
committed
feat: check multiple filenames, closes #15
1 parent 7b95f11 commit 0f08bb2

File tree

6 files changed

+65
-37
lines changed

6 files changed

+65
-37
lines changed

.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ jobs:
1818
node bin/check-coverage main1.js
1919
node bin/check-coverage to/main2.js
2020
node bin/only-covered main1.js main2.js
21+
# and can check multiple files at once
22+
node bin/check-coverage main1.js main2.js
2123
2224
- name: Check totals 🛡
2325
run: node bin/check-total --min 30

.prettierrc.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "all",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true
6+
}

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# check-code-coverage [![ci status][ci image]][ci url] ![check-code-coverage](https://img.shields.io/badge/code--coverage-100%25-brightgreen)
2+
23
> Utilities for checking the coverage produced by NYC against extra or missing files
34
45
## Use
@@ -10,6 +11,7 @@ npx only-covered foo.js bar.js
1011
```
1112

1213
Watch these short videos to see these tools in action:
14+
1315
- [Check code coverage robustly using 3rd party tool](https://youtu.be/dwU5gUG2-EM)
1416
- [Adding code coverage badge to your project](https://youtu.be/bNVRxb-MKGo)
1517
- [Show code coverage in commit status check](https://youtu.be/AAl4HmJ3YuM)
@@ -31,6 +33,12 @@ The file has to end with "main.js". You can specify part of the path, like this
3133
npx check-coverage src/app/main.js
3234
```
3335

36+
You can pass multiple filenames
37+
38+
```shell
39+
npx check-coverage main.js src/person.js
40+
```
41+
3442
## only-covered
3543

3644
Check if the coverage JSON file only the given list of files and nothing else. By default `only-covered` script reads `.nyc_output/out.json` file from the current working directory. You can specify a different file using `--from` parameter.

bin/check-coverage.js

+42-37
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,56 @@ const args = arg({
77
'--from': String, // input filename, by default ".nyc_output/out.json"
88
})
99

10-
const filename = args._[0]
11-
if (!filename) {
12-
console.error('Usage: node %s <file name>', __filename)
10+
const filenames = args._
11+
if (!filenames.length) {
12+
console.error(
13+
'Usage: node %s <file name one> <file name two> ...',
14+
__filename,
15+
)
1316
process.exit(1)
1417
}
1518

1619
const fromFilename = args['--from'] || join('.nyc_output', 'out.json')
1720
const coverageFilename = resolve(fromFilename)
18-
1921
const coverage = require(coverageFilename)
20-
const fileCoverageKey = Object.keys(coverage).find(name => {
21-
const fileCover = coverage[name]
22-
if (fileCover.path.endsWith(filename)) {
23-
return fileCover
22+
23+
filenames.forEach((filename) => {
24+
const fileCoverageKey = Object.keys(coverage).find((name) => {
25+
const fileCover = coverage[name]
26+
if (fileCover.path.endsWith(filename)) {
27+
return fileCover
28+
}
29+
})
30+
31+
if (!fileCoverageKey) {
32+
console.error(
33+
'Could not find file %s in coverage in file %s',
34+
filename,
35+
coverageFilename,
36+
)
37+
process.exit(1)
2438
}
25-
})
2639

27-
if (!fileCoverageKey) {
28-
console.error(
29-
'Could not find file %s in coverage in file %s',
30-
filename,
31-
coverageFilename
40+
const fileCoverage = coverage[fileCoverageKey]
41+
const statementCounters = fileCoverage.s
42+
const isThereUncoveredStatement = Object.keys(statementCounters).some(
43+
(k, key) => {
44+
return statementCounters[key] === 0
45+
},
3246
)
33-
process.exit(1)
34-
}
35-
36-
const fileCoverage = coverage[fileCoverageKey]
37-
const statementCounters = fileCoverage.s
38-
const isThereUncoveredStatement = Object.keys(statementCounters).some(
39-
(k, key) => {
40-
return statementCounters[key] === 0
47+
if (isThereUncoveredStatement) {
48+
console.error(
49+
'file %s has statements that were not covered by tests',
50+
fileCoverage.path,
51+
)
52+
console.log('statement counters %o', statementCounters)
53+
54+
process.exit(1)
4155
}
42-
)
43-
if (isThereUncoveredStatement) {
44-
console.error(
45-
'file %s has statements that were not covered by tests',
46-
fileCoverage.path
47-
)
48-
console.log('statement counters %o', statementCounters)
4956

50-
process.exit(1)
51-
}
52-
53-
console.log(
54-
'✅ All statements in file %s (found for %s) were covered',
55-
fileCoverage.path,
56-
filename
57-
)
57+
console.log(
58+
'✅ All statements in file %s (found for %s) were covered',
59+
fileCoverage.path,
60+
filename,
61+
)
62+
})

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"homepage": "https://github.com/bahmutov/check-code-coverage#readme",
3838
"devDependencies": {
3939
"ava": "3.11.1",
40+
"prettier": "2.1.2",
4041
"semantic-release": "^17.0.4"
4142
},
4243
"dependencies": {

0 commit comments

Comments
 (0)