|
| 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') |
0 commit comments