Skip to content

Commit 6eed7f8

Browse files
committed
feat: initial
0 parents  commit 6eed7f8

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# check-code-coverage
2+
> Utilities for checking the coverage produced by NYC against extra or missing files
3+
4+
## Use
5+
6+
```shell
7+
npm i -D check-code-coverage
8+
# check if .nyc_output/out.json has 100% code coverage for main.js
9+
npx check-coverage main.js
10+
# check if .nyc_output/out.json has files foo.js and bar.js covered and nothing else
11+
npx only-covered foo.js bar.js
12+
```

bin/check-coverage.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env node
2+
const { join } = require('path')
3+
4+
const filename = process.argv[2]
5+
if (!filename) {
6+
console.error('Usage: node %s <file name>', __filename)
7+
process.exit(1)
8+
}
9+
const coverageFilename = join(process.cwd(), '.nyc_output', 'out.json')
10+
const coverage = require(coverageFilename)
11+
const fileCoverageKey = Object.keys(coverage).find(name => {
12+
const fileCover = coverage[name]
13+
if (fileCover.path.endsWith(filename)) {
14+
return fileCover
15+
}
16+
})
17+
18+
if (!fileCoverageKey) {
19+
console.error(
20+
'Could not find file %s in coverage in file %s',
21+
filename,
22+
coverageFilename
23+
)
24+
process.exit(1)
25+
}
26+
27+
const fileCoverage = coverage[fileCoverageKey]
28+
const statementCounters = fileCoverage.s
29+
const isThereUncoveredStatement = Object.keys(statementCounters).some(
30+
(k, key) => {
31+
return statementCounters[key] === 0
32+
}
33+
)
34+
if (isThereUncoveredStatement) {
35+
console.error(
36+
'file %s has statements that were not covered by tests',
37+
fileCoverage.path
38+
)
39+
console.log('statement counters %o', statementCounters)
40+
41+
process.exit(1)
42+
}
43+
44+
console.log(
45+
'✅ All statements in file %s (found for %s) were covered',
46+
fileCoverage.path,
47+
filename
48+
)

bin/only-covered.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env node
2+
const { join } = require('path')
3+
const _ = require('lodash')
4+
5+
const filenames = process.argv.slice(2)
6+
if (!filenames.length) {
7+
console.error('Usage: node %s <file name 1> <file name 2>', __filename)
8+
process.exit(1)
9+
}
10+
11+
const shouldBeCovered = filepath =>
12+
filenames.some(name => filepath.endsWith(name))
13+
14+
const coverageFilename = join(process.cwd(), '.nyc_output', 'out.json')
15+
const coverage = require(coverageFilename)
16+
17+
const coveredFilepaths = Object.keys(coverage).map(name => coverage[name].path)
18+
19+
// console.log(coveredFilepaths)
20+
21+
const [covered, extraCoveredFiles] = _.partition(
22+
coveredFilepaths,
23+
shouldBeCovered
24+
)
25+
26+
if (extraCoveredFiles.length) {
27+
console.error('Error: found extra covered files 🔥')
28+
console.error('Expected the following files in coverage results')
29+
console.error(filenames.join('\n'))
30+
console.error('extra files covered 🔥')
31+
console.error(extraCoveredFiles.join('\n'))
32+
process.exit(1)
33+
}
34+
35+
if (covered.length < filenames.length) {
36+
console.error('Error: expected all files from the list to be covered 🔥')
37+
console.error('Expected the following files in coverage results')
38+
console.error(filenames.join('\n'))
39+
console.error('But found only these files to be covered')
40+
console.error(covered.join('\n'))
41+
42+
console.error('Files missing from the coverage 🔥')
43+
const missingFiles = filenames.filter(
44+
filename =>
45+
!covered.some(coveredFilename => coveredFilename.endsWith(filename))
46+
)
47+
console.error(missingFiles.join('\n'))
48+
49+
process.exit(1)
50+
}
51+
52+
console.log('✅ All and only expected files were covered')

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "check-code-coverage",
3+
"version": "1.0.0",
4+
"description": "Utilities for checking the coverage produced by NYC against extra or missing files",
5+
"main": "index.js",
6+
"alias": {
7+
"check-coverage": "bin/check-coverage.js",
8+
"only-covered": "bin/only-covered.js"
9+
},
10+
"files": ["bin"],
11+
"scripts": {
12+
"test": "echo \"Error: no test specified\" && exit 1"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/bahmutov/check-code-coverage.git"
17+
},
18+
"keywords": [
19+
"coverage",
20+
"code-coverage",
21+
"utility",
22+
"nyc"
23+
],
24+
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/bahmutov/check-code-coverage/issues"
28+
},
29+
"homepage": "https://github.com/bahmutov/check-code-coverage#readme"
30+
}

0 commit comments

Comments
 (0)