Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support nyc report --check-coverage #984

Merged
merged 1 commit into from Feb 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
coreyfarrell marked this conversation as resolved.
Show resolved Hide resolved
// 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