-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add thresholds for enforcing coverage percentage (#59)
- Loading branch information
Showing
12 changed files
with
402 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const { relative } = require('path') | ||
const Report = require('../report') | ||
|
||
exports.command = 'check-coverage' | ||
|
||
exports.describe = 'check whether coverage is within thresholds provided' | ||
|
||
exports.builder = function (yargs) { | ||
yargs | ||
.example('$0 check-coverage --lines 95', "check whether the JSON in c8's output folder meets the thresholds provided") | ||
} | ||
|
||
exports.handler = function (argv) { | ||
const report = Report({ | ||
include: argv.include, | ||
exclude: argv.exclude, | ||
reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter], | ||
tempDirectory: argv.tempDirectory, | ||
watermarks: argv.watermarks, | ||
resolve: argv.resolve, | ||
omitRelative: argv.omitRelative, | ||
wrapperLength: argv.wrapperLength | ||
}) | ||
exports.checkCoverages(argv, report) | ||
} | ||
|
||
exports.checkCoverages = function (argv, report) { | ||
const thresholds = { | ||
lines: argv.lines, | ||
functions: argv.functions, | ||
branches: argv.branches, | ||
statements: argv.statements | ||
} | ||
const map = report.getCoverageMapFromAllCoverageFiles() | ||
if (argv.perFile) { | ||
map.files().forEach(file => { | ||
checkCoverage(map.fileCoverageFor(file).toSummary(), thresholds, file) | ||
}) | ||
} else { | ||
checkCoverage(map.getCoverageSummary(), thresholds) | ||
} | ||
} | ||
|
||
function checkCoverage (summary, thresholds, file) { | ||
Object.keys(thresholds).forEach(key => { | ||
const coverage = summary[key].pct | ||
if (coverage < thresholds[key]) { | ||
process.exitCode = 1 | ||
if (file) { | ||
console.error( | ||
'ERROR: Coverage for ' + key + ' (' + coverage + '%) does not meet threshold (' + thresholds[key] + '%) for ' + | ||
relative('./', file).replace(/\\/g, '/') // standardize path for Windows. | ||
) | ||
} else { | ||
console.error('ERROR: Coverage for ' + key + ' (' + coverage + '%) does not meet global threshold (' + thresholds[key] + '%)') | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const Report = require('../report') | ||
|
||
exports.command = 'report' | ||
|
||
exports.describe = 'read V8 coverage data from temp and output report' | ||
|
||
exports.handler = function (argv) { | ||
exports.outputReport(argv) | ||
} | ||
|
||
exports.outputReport = function (argv) { | ||
const report = Report({ | ||
include: argv.include, | ||
exclude: argv.exclude, | ||
reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter], | ||
tempDirectory: argv.tempDirectory, | ||
watermarks: argv.watermarks, | ||
resolve: argv.resolve, | ||
omitRelative: argv.omitRelative, | ||
wrapperLength: argv.wrapperLength | ||
}) | ||
report.run() | ||
return report | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.