1 | const { relative } = require('path')
|
2 | const Report = require('../report')
|
3 |
|
4 | exports.command = 'check-coverage'
|
5 |
|
6 | exports.describe = 'check whether coverage is within thresholds provided'
|
7 |
|
8 | exports.builder = function (yargs) {
|
9 | yargs
|
10 | .example('$0 check-coverage --lines 95', "check whether the JSON in c8's output folder meets the thresholds provided")
|
11 | }
|
12 |
|
13 | exports.handler = function (argv) {
|
14 | const report = Report({
|
15 | include: argv.include,
|
16 | exclude: argv.exclude,
|
17 | reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter],
|
18 | reportsDirectory: argv['reports-dir'],
|
19 | tempDirectory: argv.tempDirectory,
|
20 | watermarks: argv.watermarks,
|
21 | resolve: argv.resolve,
|
22 | omitRelative: argv.omitRelative,
|
23 | wrapperLength: argv.wrapperLength
|
24 | })
|
25 | exports.checkCoverages(argv, report)
|
26 | }
|
27 |
|
28 | exports.checkCoverages = async function (argv, report) {
|
29 | const thresholds = {
|
30 | lines: argv.lines,
|
31 | functions: argv.functions,
|
32 | branches: argv.branches,
|
33 | statements: argv.statements
|
34 | }
|
35 | const map = await report.getCoverageMapFromAllCoverageFiles()
|
36 | if (argv.perFile) {
|
37 | map.files().forEach(file => {
|
38 | checkCoverage(map.fileCoverageFor(file).toSummary(), thresholds, file)
|
39 | })
|
40 | } else {
|
41 | checkCoverage(map.getCoverageSummary(), thresholds)
|
42 | }
|
43 | }
|
44 |
|
45 | function checkCoverage (summary, thresholds, file) {
|
46 | Object.keys(thresholds).forEach(key => {
|
47 | const coverage = summary[key].pct
|
48 | if (coverage < thresholds[key]) {
|
49 | process.exitCode = 1
|
50 | if (file) {
|
51 | console.error(
|
52 | 'ERROR: Coverage for ' + key + ' (' + coverage + '%) does not meet threshold (' + thresholds[key] + '%) for ' +
|
53 | relative('./', file).replace(/\\/g, '/')
|
54 | )
|
55 | } else {
|
56 | console.error('ERROR: Coverage for ' + key + ' (' + coverage + '%) does not meet global threshold (' + thresholds[key] + '%)')
|
57 | }
|
58 | }
|
59 | })
|
60 | }
|