Skip to content

Commit

Permalink
feat: Support nyc report --check-coverage (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs authored and coreyfarrell committed Feb 3, 2019
1 parent 29e6f5e commit dd48410
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/commands/report.js
Expand Up @@ -39,11 +39,51 @@ exports.builder = function (yargs) {
type: 'boolean',
global: false
})
.option('check-coverage', {
type: 'boolean',
default: false,
describe: 'check whether coverage is within thresholds provided',
global: false
})
.option('branches', {
default: 0,
description: 'what % of branches must be covered?',
global: false
})
.option('functions', {
default: 0,
description: 'what % of functions must be covered?',
global: false
})
.option('lines', {
default: 90,
description: 'what % of lines must be covered?',
global: false
})
.option('statements', {
default: 0,
description: 'what % of statements must be covered?',
global: false
})
.option('per-file', {
default: false,
type: 'boolean',
description: 'check thresholds per file',
global: false
})
.example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')
}

exports.handler = function (argv) {
process.env.NYC_CWD = process.cwd()
var nyc = new NYC(argv)
nyc.report()
if (argv.checkCoverage) {
nyc.checkCoverage({
lines: argv.lines,
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}, argv['per-file'])
}
}
32 changes: 32 additions & 0 deletions test/nyc-bin.js
Expand Up @@ -48,6 +48,38 @@ describe('the nyc cli', function () {
})
})

describe('report and check', function () {
it('should show coverage check along with report', function (done) {
// generate some coverage info
var args = [bin, '--silent', process.execPath, './half-covered.js']

var proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

proc.on('close', function (code) {
code.should.equal(0)
var args = [bin, 'report', '--check-coverage', '--lines=100']
var proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

var stderr = ''
proc.stderr.on('data', function (chunk) {
stderr += chunk
})

proc.on('close', function (code) {
code.should.not.equal(0)
stderr.should.equal('ERROR: Coverage for lines (50%) does not meet global threshold (100%)\n')
done()
})
})
})
})

describe('--exclude', function () {
it('should allow default exclude rules to be overridden', function (done) {
var args = [bin, '--all', '--exclude', '**/half-covered.js', process.execPath, './half-covered.js']
Expand Down

0 comments on commit dd48410

Please sign in to comment.