Skip to content

Commit dd48410

Browse files
isaacscoreyfarrell
authored andcommitted
feat: Support nyc report --check-coverage (#984)
1 parent 29e6f5e commit dd48410

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

lib/commands/report.js

+40
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,51 @@ exports.builder = function (yargs) {
3939
type: 'boolean',
4040
global: false
4141
})
42+
.option('check-coverage', {
43+
type: 'boolean',
44+
default: false,
45+
describe: 'check whether coverage is within thresholds provided',
46+
global: false
47+
})
48+
.option('branches', {
49+
default: 0,
50+
description: 'what % of branches must be covered?',
51+
global: false
52+
})
53+
.option('functions', {
54+
default: 0,
55+
description: 'what % of functions must be covered?',
56+
global: false
57+
})
58+
.option('lines', {
59+
default: 90,
60+
description: 'what % of lines must be covered?',
61+
global: false
62+
})
63+
.option('statements', {
64+
default: 0,
65+
description: 'what % of statements must be covered?',
66+
global: false
67+
})
68+
.option('per-file', {
69+
default: false,
70+
type: 'boolean',
71+
description: 'check thresholds per file',
72+
global: false
73+
})
4274
.example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')
4375
}
4476

4577
exports.handler = function (argv) {
4678
process.env.NYC_CWD = process.cwd()
4779
var nyc = new NYC(argv)
4880
nyc.report()
81+
if (argv.checkCoverage) {
82+
nyc.checkCoverage({
83+
lines: argv.lines,
84+
functions: argv.functions,
85+
branches: argv.branches,
86+
statements: argv.statements
87+
}, argv['per-file'])
88+
}
4989
}

test/nyc-bin.js

+32
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@ describe('the nyc cli', function () {
4848
})
4949
})
5050

51+
describe('report and check', function () {
52+
it('should show coverage check along with report', function (done) {
53+
// generate some coverage info
54+
var args = [bin, '--silent', process.execPath, './half-covered.js']
55+
56+
var proc = spawn(process.execPath, args, {
57+
cwd: fixturesCLI,
58+
env: env
59+
})
60+
61+
proc.on('close', function (code) {
62+
code.should.equal(0)
63+
var args = [bin, 'report', '--check-coverage', '--lines=100']
64+
var proc = spawn(process.execPath, args, {
65+
cwd: fixturesCLI,
66+
env: env
67+
})
68+
69+
var stderr = ''
70+
proc.stderr.on('data', function (chunk) {
71+
stderr += chunk
72+
})
73+
74+
proc.on('close', function (code) {
75+
code.should.not.equal(0)
76+
stderr.should.equal('ERROR: Coverage for lines (50%) does not meet global threshold (100%)\n')
77+
done()
78+
})
79+
})
80+
})
81+
})
82+
5183
describe('--exclude', function () {
5284
it('should allow default exclude rules to be overridden', function (done) {
5385
var args = [bin, '--all', '--exclude', '**/half-covered.js', process.execPath, './half-covered.js']

0 commit comments

Comments
 (0)