Skip to content

Commit db2e60c

Browse files
committed
check if extra files are covered
1 parent b95f57d commit db2e60c

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

Diff for: .circleci/config.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,11 @@ workflows:
195195
command: npx nyc report --check-coverage true --lines 100 --include unit-utils.js
196196
working_directory: examples/same-folder
197197
- run:
198-
command: node ../../scripts/check-coverage main.js
199-
working_directory: examples/same-folder
200-
- run:
201-
command: node ../../scripts/check-coverage unit-utils.js
198+
name: Check code coverage 📈
199+
command: |
200+
node ../../scripts/check-coverage main.js
201+
node ../../scripts/check-coverage unit-utils.js
202+
node ../../scripts/only-covered main.js unit-utils.js
202203
working_directory: examples/same-folder
203204

204205
- publish:

Diff for: scripts/only-covered.js

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

0 commit comments

Comments
 (0)