|
| 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