Skip to content

Commit 4205f2f

Browse files
authored
feat: --100 (bcoe#332)
1 parent 1866b93 commit 4205f2f

File tree

7 files changed

+205
-33
lines changed

7 files changed

+205
-33
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ To check thresholds on a per-file basis run:
8080
c8 check-coverage --lines 95 --per-file
8181
```
8282

83+
If you want to check for 100% coverage across all dimensions, use `--100`:
84+
85+
```shell
86+
c8 --100 npm test
87+
```
88+
89+
Is equivalent to
90+
91+
```shell
92+
c8 --check-coverage --lines 100 --functions 100 --branches 100 --statements 100 npm test
93+
```
94+
95+
The `--100` flag can be set for the `check-coverage` as well:
96+
97+
```shell
98+
c8 check-coverage --100
99+
```
100+
83101
## Ignoring Uncovered Lines, Functions, and Blocks
84102

85103
Sometimes you might find yourself wanting to ignore uncovered portions of your

lib/commands/check-coverage.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ exports.builder = function (yargs) {
1111
}
1212

1313
exports.handler = function (argv) {
14+
// TODO: this is a workaround until yargs gets upgraded to v17, see https://github.com/bcoe/c8/pull/332#discussion_r721636191
15+
if (argv['100']) {
16+
argv.lines = 100
17+
argv.functions = 100
18+
argv.branches = 100
19+
argv.statements = 100
20+
}
21+
1422
const report = Report({
1523
include: argv.include,
1624
exclude: argv.exclude,

lib/commands/report.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ exports.handler = async function (argv) {
1010
}
1111

1212
exports.outputReport = async function (argv) {
13+
// TODO: this is a workaround until yargs gets upgraded to v17, see https://github.com/bcoe/c8/pull/332#discussion_r721636191
14+
if (argv['100']) {
15+
argv.checkCoverage = 100
16+
argv.lines = 100
17+
argv.functions = 100
18+
argv.branches = 100
19+
argv.statements = 100
20+
}
1321
const report = Report({
1422
include: argv.include,
1523
exclude: argv.exclude,

lib/parse-args.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ function buildYargs (withCommands = false) {
107107
description: 'check thresholds per file',
108108
type: 'boolean'
109109
})
110+
.option('100', {
111+
default: false,
112+
group: 'Coverage thresholds',
113+
description: 'shortcut for --check-coverage --lines 100 --functions 100 --branches 100 --statements 100',
114+
type: 'boolean'
115+
})
110116
.option('temp-directory', {
111117
describe: 'directory V8 coverage data is written to and read from',
112118
default: process.env.NODE_V8_COVERAGE
@@ -145,6 +151,19 @@ function buildYargs (withCommands = false) {
145151
})
146152
.epilog('visit https://git.io/vHysA for list of available reporters')
147153

154+
// TODO: enable once yargs upgraded to v17: https://github.com/bcoe/c8/pull/332#discussion_r721636191
155+
// yargs.middleware((argv) => {
156+
// if (!argv['100']) return argv
157+
158+
// return {
159+
// ...argv,
160+
// branches: 100,
161+
// functions: 100,
162+
// lines: 100,
163+
// statements: 100,
164+
// }
165+
// })
166+
148167
const checkCoverage = require('./commands/check-coverage')
149168
const report = require('./commands/report')
150169
if (withCommands) {

test/integration.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,32 @@ describe('c8', () => {
211211
status.should.equal(1)
212212
output.toString('utf8').should.matchSnapshot()
213213
})
214+
215+
it('--100', () => {
216+
const { output, status } = spawnSync(nodePath, [
217+
c8Path,
218+
'--exclude="test/*.js"',
219+
'--temp-directory=tmp/check-coverage',
220+
'--100',
221+
nodePath,
222+
require.resolve('./fixtures/normal')
223+
])
224+
225+
status.should.equal(1)
226+
output.toString('utf8').should.matchSnapshot()
227+
})
228+
229+
it('check-coverage command with --100', () => {
230+
const { output, status } = spawnSync(nodePath, [
231+
c8Path,
232+
'check-coverage',
233+
'--exclude="test/*.js"',
234+
'--temp-directory=tmp/check-coverage',
235+
'--100'
236+
])
237+
status.should.equal(1)
238+
output.toString('utf8').should.matchSnapshot()
239+
})
214240
})
215241

216242
describe('report', () => {

test/integration.js.snap

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,52 @@ All files | 100 | 100 | 100 | 100 |
152152
,"
153153
`;
154154

155+
exports[`c8 check-coverage --100 1`] = `
156+
",hey
157+
i am a line of code
158+
what
159+
hey
160+
what
161+
hey
162+
what
163+
hey
164+
-----------|---------|----------|---------|---------|-------------------
165+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
166+
-----------|---------|----------|---------|---------|-------------------
167+
All files | 83.33 | 85.71 | 60 | 83.33 |
168+
async.js | 100 | 100 | 100 | 100 |
169+
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
170+
-----------|---------|----------|---------|---------|-------------------
171+
,ERROR: Coverage for lines (83.33%) does not meet global threshold (100%)
172+
ERROR: Coverage for functions (60%) does not meet global threshold (100%)
173+
ERROR: Coverage for branches (85.71%) does not meet global threshold (100%)
174+
ERROR: Coverage for statements (83.33%) does not meet global threshold (95%)
175+
"
176+
`;
177+
178+
exports[`c8 check-coverage --100 1`] = `
179+
",hey
180+
i am a line of code
181+
what
182+
hey
183+
what
184+
hey
185+
what
186+
hey
187+
-----------|---------|----------|---------|---------|-------------------
188+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
189+
-----------|---------|----------|---------|---------|-------------------
190+
All files | 83.33 | 85.71 | 60 | 83.33 |
191+
async.js | 100 | 100 | 100 | 100 |
192+
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
193+
-----------|---------|----------|---------|---------|-------------------
194+
,ERROR: Coverage for lines (83.33%) does not meet global threshold (100%)
195+
ERROR: Coverage for functions (60%) does not meet global threshold (100%)
196+
ERROR: Coverage for branches (85.71%) does not meet global threshold (100%)
197+
ERROR: Coverage for statements (83.33%) does not meet global threshold (100%)
198+
"
199+
`;
200+
155201
exports[`c8 check-coverage allows --check-coverage when executing script 1`] = `
156202
",hey
157203
i am a line of code
@@ -181,6 +227,22 @@ ERROR: Coverage for statements (75%) does not meet threshold (95%) for test/fixt
181227
"
182228
`;
183229

230+
exports[`c8 check-coverage check-coverage --100 1`] = `
231+
",,ERROR: Coverage for lines (83.33%) does not meet global threshold (100%)
232+
ERROR: Coverage for functions (60%) does not meet global threshold (100%)
233+
ERROR: Coverage for branches (85.71%) does not meet global threshold (100%)
234+
ERROR: Coverage for statements (83.33%) does not meet global threshold (95%)
235+
"
236+
`;
237+
238+
exports[`c8 check-coverage check-coverage command with --100 1`] = `
239+
",,ERROR: Coverage for lines (83.33%) does not meet global threshold (100%)
240+
ERROR: Coverage for functions (60%) does not meet global threshold (100%)
241+
ERROR: Coverage for branches (85.71%) does not meet global threshold (100%)
242+
ERROR: Coverage for statements (83.33%) does not meet global threshold (100%)
243+
"
244+
`;
245+
184246
exports[`c8 check-coverage exits with 0 if coverage within threshold 1`] = `",,"`;
185247

186248
exports[`c8 check-coverage exits with 1 if coverage is below threshold 1`] = `

test/integration.js_10.snap

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,29 @@ All files | 0 | 0 | 0 | 0 |
140140
"
141141
`;
142142

143+
exports[`c8 check-coverage --100 1`] = `
144+
",hey
145+
i am a line of code
146+
what
147+
hey
148+
what
149+
hey
150+
what
151+
hey
152+
-----------|---------|----------|---------|---------|-------------------
153+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
154+
-----------|---------|----------|---------|---------|-------------------
155+
All files | 83.33 | 85.71 | 66.66 | 83.33 |
156+
async.js | 100 | 100 | 100 | 100 |
157+
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
158+
-----------|---------|----------|---------|---------|-------------------
159+
,ERROR: Coverage for lines (83.33%) does not meet global threshold (100%)
160+
ERROR: Coverage for functions (66.66%) does not meet global threshold (100%)
161+
ERROR: Coverage for branches (85.71%) does not meet global threshold (100%)
162+
ERROR: Coverage for statements (83.33%) does not meet global threshold (100%)
163+
"
164+
`;
165+
143166
exports[`c8 check-coverage allows --check-coverage when executing script 1`] = `
144167
",hey
145168
i am a line of code
@@ -152,40 +175,40 @@ hey
152175
--------------------------|---------|----------|---------|---------|--------------------------------
153176
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
154177
--------------------------|---------|----------|---------|---------|--------------------------------
155-
All files | 73.96 | 59.75 | 62.5 | 73.96 |
178+
All files | 73.37 | 59.03 | 62.5 | 73.37 |
156179
bin | 78.84 | 60 | 66.66 | 78.84 |
157180
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
158-
lib | 77.19 | 54.38 | 72 | 77.19 |
181+
lib | 77.88 | 54.38 | 72 | 77.88 |
159182
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
160-
parse-args.js | 96.8 | 58.33 | 100 | 96.8 | 142-143,151-152,165-166
183+
parse-args.js | 97.1 | 58.33 | 100 | 97.1 | 148-149,170-171,184-185
161184
report.js | 75.31 | 58.33 | 78.57 | 75.31 | ...249,276-277,283-285,306-311
162185
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
163-
lib/commands | 46.23 | 75 | 16.66 | 46.23 |
164-
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
165-
report.js | 93.75 | 71.42 | 50 | 93.75 | 9-10
186+
lib/commands | 41.28 | 66.66 | 16.66 | 41.28 |
187+
check-coverage.js | 18.84 | 100 | 0 | 18.84 | 9-11,14-35,38-52,54-69
188+
report.js | 80 | 62.5 | 50 | 80 | 9-10,15-20
166189
test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 |
167190
async.js | 100 | 100 | 100 | 100 |
168191
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
169192
--------------------------|---------|----------|---------|---------|--------------------------------
170-
,ERROR: Coverage for lines (73.96%) does not meet global threshold (101%)
171-
ERROR: Coverage for branches (59.75%) does not meet global threshold (82%)
172-
ERROR: Coverage for statements (73.96%) does not meet global threshold (95%)
193+
,ERROR: Coverage for lines (73.37%) does not meet global threshold (101%)
194+
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
195+
ERROR: Coverage for statements (73.37%) does not meet global threshold (95%)
173196
"
174197
`;
175198

176199
exports[`c8 check-coverage allows threshold to be applied on per-file basis 1`] = `
177200
",,ERROR: Coverage for lines (78.84%) does not meet threshold (101%) for bin/c8.js
178201
ERROR: Coverage for branches (60%) does not meet threshold (82%) for bin/c8.js
179202
ERROR: Coverage for statements (78.84%) does not meet threshold (95%) for bin/c8.js
180-
ERROR: Coverage for lines (21.31%) does not meet threshold (101%) for lib/commands/check-coverage.js
181-
ERROR: Coverage for statements (21.31%) does not meet threshold (95%) for lib/commands/check-coverage.js
182-
ERROR: Coverage for lines (93.75%) does not meet threshold (101%) for lib/commands/report.js
183-
ERROR: Coverage for branches (71.42%) does not meet threshold (82%) for lib/commands/report.js
184-
ERROR: Coverage for statements (93.75%) does not meet threshold (95%) for lib/commands/report.js
203+
ERROR: Coverage for lines (18.84%) does not meet threshold (101%) for lib/commands/check-coverage.js
204+
ERROR: Coverage for statements (18.84%) does not meet threshold (95%) for lib/commands/check-coverage.js
205+
ERROR: Coverage for lines (80%) does not meet threshold (101%) for lib/commands/report.js
206+
ERROR: Coverage for branches (62.5%) does not meet threshold (82%) for lib/commands/report.js
207+
ERROR: Coverage for statements (80%) does not meet threshold (95%) for lib/commands/report.js
185208
ERROR: Coverage for lines (90%) does not meet threshold (101%) for lib/is-cjs-esm-bridge.js
186209
ERROR: Coverage for branches (25%) does not meet threshold (82%) for lib/is-cjs-esm-bridge.js
187210
ERROR: Coverage for statements (90%) does not meet threshold (95%) for lib/is-cjs-esm-bridge.js
188-
ERROR: Coverage for lines (96.8%) does not meet threshold (101%) for lib/parse-args.js
211+
ERROR: Coverage for lines (97.1%) does not meet threshold (101%) for lib/parse-args.js
189212
ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/parse-args.js
190213
ERROR: Coverage for lines (75.31%) does not meet threshold (101%) for lib/report.js
191214
ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/report.js
@@ -199,12 +222,20 @@ ERROR: Coverage for statements (75%) does not meet threshold (95%) for test/fixt
199222
"
200223
`;
201224

225+
exports[`c8 check-coverage check-coverage command with --100 1`] = `
226+
",,ERROR: Coverage for lines (77.22%) does not meet global threshold (100%)
227+
ERROR: Coverage for functions (66.66%) does not meet global threshold (100%)
228+
ERROR: Coverage for branches (62.35%) does not meet global threshold (100%)
229+
ERROR: Coverage for statements (77.22%) does not meet global threshold (100%)
230+
"
231+
`;
232+
202233
exports[`c8 check-coverage exits with 0 if coverage within threshold 1`] = `",,"`;
203234

204235
exports[`c8 check-coverage exits with 1 if coverage is below threshold 1`] = `
205-
",,ERROR: Coverage for lines (73.96%) does not meet global threshold (101%)
206-
ERROR: Coverage for branches (59.75%) does not meet global threshold (82%)
207-
ERROR: Coverage for statements (73.96%) does not meet global threshold (95%)
236+
",,ERROR: Coverage for lines (73.37%) does not meet global threshold (101%)
237+
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
238+
ERROR: Coverage for statements (73.37%) does not meet global threshold (95%)
208239
"
209240
`;
210241

@@ -287,17 +318,17 @@ exports[`c8 report generates report from existing temporary files 1`] = `
287318
",--------------------------|---------|----------|---------|---------|--------------------------------
288319
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
289320
--------------------------|---------|----------|---------|---------|--------------------------------
290-
All files | 73.96 | 59.75 | 62.5 | 73.96 |
321+
All files | 73.37 | 59.03 | 62.5 | 73.37 |
291322
bin | 78.84 | 60 | 66.66 | 78.84 |
292323
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
293-
lib | 77.19 | 54.38 | 72 | 77.19 |
324+
lib | 77.88 | 54.38 | 72 | 77.88 |
294325
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
295-
parse-args.js | 96.8 | 58.33 | 100 | 96.8 | 142-143,151-152,165-166
326+
parse-args.js | 97.1 | 58.33 | 100 | 97.1 | 148-149,170-171,184-185
296327
report.js | 75.31 | 58.33 | 78.57 | 75.31 | ...249,276-277,283-285,306-311
297328
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
298-
lib/commands | 46.23 | 75 | 16.66 | 46.23 |
299-
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
300-
report.js | 93.75 | 71.42 | 50 | 93.75 | 9-10
329+
lib/commands | 41.28 | 66.66 | 16.66 | 41.28 |
330+
check-coverage.js | 18.84 | 100 | 0 | 18.84 | 9-11,14-35,38-52,54-69
331+
report.js | 80 | 62.5 | 50 | 80 | 9-10,15-20
301332
test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 |
302333
async.js | 100 | 100 | 100 | 100 |
303334
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
@@ -309,24 +340,24 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = `
309340
",--------------------------|---------|----------|---------|---------|--------------------------------
310341
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
311342
--------------------------|---------|----------|---------|---------|--------------------------------
312-
All files | 73.96 | 59.75 | 62.5 | 73.96 |
343+
All files | 73.37 | 59.03 | 62.5 | 73.37 |
313344
bin | 78.84 | 60 | 66.66 | 78.84 |
314345
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
315-
lib | 77.19 | 54.38 | 72 | 77.19 |
346+
lib | 77.88 | 54.38 | 72 | 77.88 |
316347
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
317-
parse-args.js | 96.8 | 58.33 | 100 | 96.8 | 142-143,151-152,165-166
348+
parse-args.js | 97.1 | 58.33 | 100 | 97.1 | 148-149,170-171,184-185
318349
report.js | 75.31 | 58.33 | 78.57 | 75.31 | ...249,276-277,283-285,306-311
319350
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
320-
lib/commands | 46.23 | 75 | 16.66 | 46.23 |
321-
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
322-
report.js | 93.75 | 71.42 | 50 | 93.75 | 9-10
351+
lib/commands | 41.28 | 66.66 | 16.66 | 41.28 |
352+
check-coverage.js | 18.84 | 100 | 0 | 18.84 | 9-11,14-35,38-52,54-69
353+
report.js | 80 | 62.5 | 50 | 80 | 9-10,15-20
323354
test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 |
324355
async.js | 100 | 100 | 100 | 100 |
325356
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
326357
--------------------------|---------|----------|---------|---------|--------------------------------
327-
,ERROR: Coverage for lines (73.96%) does not meet global threshold (101%)
328-
ERROR: Coverage for branches (59.75%) does not meet global threshold (82%)
329-
ERROR: Coverage for statements (73.96%) does not meet global threshold (95%)
358+
,ERROR: Coverage for lines (73.37%) does not meet global threshold (101%)
359+
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
360+
ERROR: Coverage for statements (73.37%) does not meet global threshold (95%)
330361
"
331362
`;
332363

0 commit comments

Comments
 (0)