Skip to content

Commit

Permalink
refactor: Increasing code coverage of lib/parse-args.js to 100%. (#447)
Browse files Browse the repository at this point in the history
Test: Adding a test case for NODE_V8_COVERAGE and changing the describe block title. (#447)
  • Loading branch information
mcknasty committed Jan 28, 2023
1 parent ddabd7c commit ba0acbc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 39 deletions.
7 changes: 3 additions & 4 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ const { applyExtends } = require('yargs/helpers')
const parser = require('yargs-parser')

function buildYargs (withCommands = false) {
let tempDir = './coverage/tmp'
if (typeof process.env.NODE_V8_COVERAGE === 'string') {
tempDir = process.env.NODE_V8_COVERAGE
if (typeof process.env.NODE_V8_COVERAGE !== 'string') {
process.env.NODE_V8_COVERAGE = process.cwd() + '/coverage/tmp'
}

const yargs = Yargs([])
Expand Down Expand Up @@ -130,7 +129,7 @@ function buildYargs (withCommands = false) {
})
.option('temp-directory', {
describe: 'directory V8 coverage data is written to and read from',
default: tempDir,
default: process.env.NODE_V8_COVERAGE,
type: 'string'
})
.option('clean', {
Expand Down
32 changes: 23 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/integration.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ All files | 1.91 | 12 | 6.25 | 1.91
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/lib | 0 | 0 | 0 | 0 |
is-cjs-esm-bridge.js | 0 | 0 | 0 | 0 | 1-10
parse-args.js | 0 | 0 | 0 | 0 | 1-212
parse-args.js | 0 | 0 | 0 | 0 | 1-211
report.js | 0 | 0 | 0 | 0 | 1-340
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-100
c8/lib/commands | 0 | 0 | 0 | 0 |
Expand Down
48 changes: 24 additions & 24 deletions test/integration.js_10.snap
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ All files | 1.91 | 12 | 8.16 | 1.91
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/lib | 0 | 0 | 0 | 0 |
is-cjs-esm-bridge.js | 0 | 0 | 0 | 0 | 1-10
parse-args.js | 0 | 0 | 0 | 0 | 1-212
parse-args.js | 0 | 0 | 0 | 0 | 1-211
report.js | 0 | 0 | 0 | 0 | 1-340
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-100
c8/lib/commands | 0 | 0 | 0 | 0 |
Expand Down Expand Up @@ -257,12 +257,12 @@ hey
--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 74.33 | 60.49 | 62.5 | 74.33 |
All files | 74.06 | 59.75 | 62.5 | 74.06 |
bin | 78.84 | 60 | 66.66 | 78.84 |
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
lib | 79 | 56.36 | 72 | 79 |
lib | 78.66 | 55.35 | 72 | 78.66 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 99.05 | 75 | 100 | 99.05 | 180-181
parse-args.js | 98.1 | 66.66 | 100 | 98.1 | 11-12,179-180
report.js | 76.17 | 57.89 | 80 | 76.17 | ...284,290-292,313-318,329-330
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
Expand All @@ -272,9 +272,9 @@ All files | 74.33 | 60.49 | 62.5 | 74.33 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
--------------------------|---------|----------|---------|---------|--------------------------------
,ERROR: Coverage for lines (74.33%) does not meet global threshold (101%)
ERROR: Coverage for branches (60.49%) does not meet global threshold (82%)
ERROR: Coverage for statements (74.33%) does not meet global threshold (95%)
,ERROR: Coverage for lines (74.06%) does not meet global threshold (101%)
ERROR: Coverage for branches (59.75%) does not meet global threshold (82%)
ERROR: Coverage for statements (74.06%) does not meet global threshold (95%)
"
`;

Expand All @@ -290,8 +290,8 @@ ERROR: Coverage for statements (80.48%) does not meet threshold (95%) for lib/co
ERROR: Coverage for lines (90%) does not meet threshold (101%) for lib/is-cjs-esm-bridge.js
ERROR: Coverage for branches (25%) does not meet threshold (82%) for lib/is-cjs-esm-bridge.js
ERROR: Coverage for statements (90%) does not meet threshold (95%) for lib/is-cjs-esm-bridge.js
ERROR: Coverage for lines (99.05%) does not meet threshold (101%) for lib/parse-args.js
ERROR: Coverage for branches (75%) does not meet threshold (82%) for lib/parse-args.js
ERROR: Coverage for lines (98.1%) does not meet threshold (101%) for lib/parse-args.js
ERROR: Coverage for branches (66.66%) does not meet threshold (82%) for lib/parse-args.js
ERROR: Coverage for lines (76.17%) does not meet threshold (101%) for lib/report.js
ERROR: Coverage for branches (57.89%) does not meet threshold (82%) for lib/report.js
ERROR: Coverage for statements (76.17%) does not meet threshold (95%) for lib/report.js
Expand All @@ -305,19 +305,19 @@ ERROR: Coverage for statements (75%) does not meet threshold (95%) for test/fixt
`;

exports[`c8 check-coverage check-coverage command with --100 1`] = `
",,ERROR: Coverage for lines (78.04%) does not meet global threshold (100%)
",,ERROR: Coverage for lines (77.79%) does not meet global threshold (100%)
ERROR: Coverage for functions (66.66%) does not meet global threshold (100%)
ERROR: Coverage for branches (63.85%) does not meet global threshold (100%)
ERROR: Coverage for statements (78.04%) does not meet global threshold (100%)
ERROR: Coverage for branches (63.09%) does not meet global threshold (100%)
ERROR: Coverage for statements (77.79%) does not meet global threshold (100%)
"
`;

exports[`c8 check-coverage exits with 0 if coverage within threshold 1`] = `",,"`;

exports[`c8 check-coverage exits with 1 if coverage is below threshold 1`] = `
",,ERROR: Coverage for lines (74.33%) does not meet global threshold (101%)
ERROR: Coverage for branches (60.49%) does not meet global threshold (82%)
ERROR: Coverage for statements (74.33%) does not meet global threshold (95%)
",,ERROR: Coverage for lines (74.06%) does not meet global threshold (101%)
ERROR: Coverage for branches (59.75%) does not meet global threshold (82%)
ERROR: Coverage for statements (74.06%) does not meet global threshold (95%)
"
`;

Expand Down Expand Up @@ -404,12 +404,12 @@ exports[`c8 report generates report from existing temporary files 1`] = `
",--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 74.33 | 60.49 | 62.5 | 74.33 |
All files | 74.06 | 59.75 | 62.5 | 74.06 |
bin | 78.84 | 60 | 66.66 | 78.84 |
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
lib | 79 | 56.36 | 72 | 79 |
lib | 78.66 | 55.35 | 72 | 78.66 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 99.05 | 75 | 100 | 99.05 | 180-181
parse-args.js | 98.1 | 66.66 | 100 | 98.1 | 11-12,179-180
report.js | 76.17 | 57.89 | 80 | 76.17 | ...284,290-292,313-318,329-330
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
Expand All @@ -426,12 +426,12 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = `
",--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 74.33 | 60.49 | 62.5 | 74.33 |
All files | 74.06 | 59.75 | 62.5 | 74.06 |
bin | 78.84 | 60 | 66.66 | 78.84 |
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
lib | 79 | 56.36 | 72 | 79 |
lib | 78.66 | 55.35 | 72 | 78.66 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 99.05 | 75 | 100 | 99.05 | 180-181
parse-args.js | 98.1 | 66.66 | 100 | 98.1 | 11-12,179-180
report.js | 76.17 | 57.89 | 80 | 76.17 | ...284,290-292,313-318,329-330
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
Expand All @@ -441,9 +441,9 @@ All files | 74.33 | 60.49 | 62.5 | 74.33 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
--------------------------|---------|----------|---------|---------|--------------------------------
,ERROR: Coverage for lines (74.33%) does not meet global threshold (101%)
ERROR: Coverage for branches (60.49%) does not meet global threshold (82%)
ERROR: Coverage for statements (74.33%) does not meet global threshold (95%)
,ERROR: Coverage for lines (74.06%) does not meet global threshold (101%)
ERROR: Coverage for branches (59.75%) does not meet global threshold (82%)
ERROR: Coverage for statements (74.06%) does not meet global threshold (95%)
"
`;
Expand Down
10 changes: 9 additions & 1 deletion test/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('parse-args', () => {
})
})

describe('with NODE_V8_COVERAGE already set', () => {
describe('NODE_V8_COVERAGE tests', () => {
it('should not override it', () => {
const NODE_V8_COVERAGE = process.env.NODE_V8_COVERAGE
process.env.NODE_V8_COVERAGE = './coverage/tmp_'
Expand All @@ -36,6 +36,14 @@ describe('parse-args', () => {
argv.tempDirectory.endsWith('/coverage/tmp_').should.be.equal(true)
process.env.NODE_V8_COVERAGE = NODE_V8_COVERAGE
})
it('should set it if undefined', () => {
const NODE_V8_COVERAGE = process.env.NODE_V8_COVERAGE
delete process.env.NODE_V8_COVERAGE
process.argv = ['node', 'c8', '--foo=99', 'my-app', '--help']
const argv = buildYargs().parse(hideInstrumenteeArgs())
argv.tempDirectory.endsWith('/coverage/tmp').should.be.equal(true)
process.env.NODE_V8_COVERAGE = NODE_V8_COVERAGE
})
})

describe('--config', () => {
Expand Down

0 comments on commit ba0acbc

Please sign in to comment.