Skip to content

Commit

Permalink
feat: add --extension option (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey authored Dec 30, 2021
1 parent 8d6e64c commit ff01cd8
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 38 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Here is a list of common options. Run `c8 --help` for the full list and document
| `--all` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `boolean` | `false` |
| `--src` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[process.cwd()]`|
| `-n`, `--include` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[]` (include all files) |
| `-x`, `--exclude` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | [list](https://github.com/istanbuljs/schema/blob/main/default-exclude.js) |
| `-x`, `--exclude` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | [list](https://github.com/istanbuljs/schema/blob/master/default-exclude.js) |
| `-e`, `--extension` | only files matching these extensions will show coverage | `string | Array<string>` | [list](https://github.com/istanbuljs/schema/blob/master/default-extension.js) |
| `--skip-full` | do not show files with 100% statement, branch, and function coverage | `boolean` | `false` |
| `--check-coverage` | check whether coverage is within thresholds provided | `boolean` | `false` |
| `--temp-directory` | directory V8 coverage data is written to and read from | `string` | `process.env.NODE_V8_COVERAGE` |
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type Watermark = [number, number];
export declare class Report {
constructor(opts: {
exclude?: string | string[],
extension?: string | string[],
excludeAfterRemap?: boolean,
include?: string | string[],
reporter: string[],
Expand Down
1 change: 1 addition & 0 deletions lib/commands/check-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exports.handler = function (argv) {
const report = Report({
include: argv.include,
exclude: argv.exclude,
extension: argv.extension,
reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter],
reportsDirectory: argv['reports-dir'],
tempDirectory: argv.tempDirectory,
Expand Down
1 change: 1 addition & 0 deletions lib/commands/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports.outputReport = async function (argv) {
const report = Report({
include: argv.include,
exclude: argv.exclude,
extension: argv.extension,
excludeAfterRemap: argv.excludeAfterRemap,
reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter],
reportsDirectory: argv['reports-dir'],
Expand Down
7 changes: 7 additions & 0 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const defaultExclude = require('@istanbuljs/schema/default-exclude')
const defaultExtension = require('@istanbuljs/schema/default-extension')

This comment has been minimized.

Copy link
@make-github-pseudonymous-again

make-github-pseudonymous-again Jan 3, 2022

Contributor

I am getting the following error with c8 7.11.0:

Error: Cannot find module '@istanbuljs/schema/default-extension'

This dependency is absent from package.json.

This comment has been minimized.

Copy link
@make-github-pseudonymous-again

make-github-pseudonymous-again Jan 3, 2022

Contributor

Oh wait, I see! @istanbuljs/schema is in package.json. Must be a Node 17 require resolve issue then.

This comment has been minimized.

Copy link
@make-github-pseudonymous-again

make-github-pseudonymous-again Jan 3, 2022

Contributor

OK. Managed to fix this by regenerating my yarn.lock file.

const findUp = require('find-up')
const { readFileSync } = require('fs')
const Yargs = require('yargs/yargs')
Expand Down Expand Up @@ -58,6 +59,12 @@ function buildYargs (withCommands = false) {
group: 'Reporting options',
describe: 'a list of specific files and directories that should be excluded from coverage (glob patterns are supported)'
})
.option('extension', {
alias: 'e',
default: defaultExtension,
group: 'Reporting options',
describe: 'a list of specific file extensions that should be covered'
})
.option('exclude-after-remap', {
alias: 'a',
type: 'boolean',
Expand Down
2 changes: 2 additions & 0 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const debuglog = util.debuglog('c8')
class Report {
constructor ({
exclude,
extension,
excludeAfterRemap,
include,
reporter,
Expand All @@ -38,6 +39,7 @@ class Report {
this.exclude = new Exclude({
exclude: exclude,
include: include,
extension: extension,
relativePath: !allowExternal,
excludeNodeModules: excludeNodeModules
})
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

24 changes: 24 additions & 0 deletions test/fixtures/custom-ext.special
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require('./async')

console.info('i am a line of code')

function apple (awesome) {
if (false || true) {
console.info('what')
}
if (true || false) {
console.log('hey')
}
}

function missed () {

}

function missed2 () {

}

apple()
apple()
apple()
16 changes: 16 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,20 @@ describe('c8', () => {
output.toString('utf8').should.matchSnapshot()
})
})

describe('--extension', () => {
it('includes coverage when extensions specified', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--extension=.js',
'--extension=.special',
'--temp-directory=tmp/extension',
'--clean=false',
nodePath,
require.resolve('./fixtures/custom-ext.special')
])
output.toString('utf8').should.matchSnapshot()
})
})
})
19 changes: 19 additions & 0 deletions test/integration.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ All files | 100 | 100 | 100 | 100 |
,"
`;

exports[`c8 --extension includes coverage when extensions specified 1`] = `
",hey
i am a line of code
what
hey
what
hey
what
hey
--------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------|---------|----------|---------|---------|-------------------
All files | 83.33 | 85.71 | 60 | 83.33 |
async.js | 100 | 100 | 100 | 100 |
custom-ext.special | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
--------------------|---------|----------|---------|---------|-------------------
,"
`;

exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
",bar foo
------------|---------|----------|---------|---------|-------------------
Expand Down
91 changes: 55 additions & 36 deletions test/integration.js_10.snap
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ All files | 100 | 100 | 100 | 100 |
,"
`;

exports[`c8 --extension includes coverage when extensions specified 1`] = `
",hey
i am a line of code
what
hey
what
hey
what
hey
--------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------|---------|----------|---------|---------|-------------------
All files | 83.33 | 85.71 | 66.66 | 83.33 |
async.js | 100 | 100 | 100 | 100 |
custom-ext.special | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
--------------------|---------|----------|---------|---------|-------------------
,"
`;

exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
",----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
Expand Down Expand Up @@ -175,44 +194,44 @@ hey
--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 73.37 | 59.03 | 62.5 | 73.37 |
All files | 73.6 | 59.03 | 62.5 | 73.6 |
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 | 77.88 | 54.38 | 72 | 77.88 |
lib | 78.19 | 54.38 | 72 | 78.19 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 97.1 | 58.33 | 100 | 97.1 | 148-149,170-171,184-185
report.js | 75.31 | 58.33 | 78.57 | 75.31 | ...249,276-277,283-285,306-311
parse-args.js | 97.19 | 58.33 | 100 | 97.19 | 155-156,177-178,191-192
report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 41.28 | 66.66 | 16.66 | 41.28 |
check-coverage.js | 18.84 | 100 | 0 | 18.84 | 9-11,14-35,38-52,54-69
report.js | 80 | 62.5 | 50 | 80 | 9-10,15-20
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70
report.js | 80.48 | 62.5 | 50 | 80.48 | 9-10,15-20
test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
--------------------------|---------|----------|---------|---------|--------------------------------
,ERROR: Coverage for lines (73.37%) does not meet global threshold (101%)
,ERROR: Coverage for lines (73.6%) does not meet global threshold (101%)
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
ERROR: Coverage for statements (73.37%) does not meet global threshold (95%)
ERROR: Coverage for statements (73.6%) does not meet global threshold (95%)
"
`;

exports[`c8 check-coverage allows threshold to be applied on per-file basis 1`] = `
",,ERROR: Coverage for lines (78.84%) does not meet threshold (101%) for bin/c8.js
ERROR: Coverage for branches (60%) does not meet threshold (82%) for bin/c8.js
ERROR: Coverage for statements (78.84%) does not meet threshold (95%) for bin/c8.js
ERROR: Coverage for lines (18.84%) does not meet threshold (101%) for lib/commands/check-coverage.js
ERROR: Coverage for statements (18.84%) does not meet threshold (95%) for lib/commands/check-coverage.js
ERROR: Coverage for lines (80%) does not meet threshold (101%) for lib/commands/report.js
ERROR: Coverage for lines (18.57%) does not meet threshold (101%) for lib/commands/check-coverage.js
ERROR: Coverage for statements (18.57%) does not meet threshold (95%) for lib/commands/check-coverage.js
ERROR: Coverage for lines (80.48%) does not meet threshold (101%) for lib/commands/report.js
ERROR: Coverage for branches (62.5%) does not meet threshold (82%) for lib/commands/report.js
ERROR: Coverage for statements (80%) does not meet threshold (95%) for lib/commands/report.js
ERROR: Coverage for statements (80.48%) does not meet threshold (95%) for lib/commands/report.js
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 (97.1%) does not meet threshold (101%) for lib/parse-args.js
ERROR: Coverage for lines (97.19%) does not meet threshold (101%) for lib/parse-args.js
ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/parse-args.js
ERROR: Coverage for lines (75.31%) does not meet threshold (101%) for lib/report.js
ERROR: Coverage for lines (75.47%) does not meet threshold (101%) for lib/report.js
ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/report.js
ERROR: Coverage for statements (75.31%) does not meet threshold (95%) for lib/report.js
ERROR: Coverage for statements (75.47%) does not meet threshold (95%) for lib/report.js
ERROR: Coverage for lines (45%) does not meet threshold (101%) for lib/source-map-from-file.js
ERROR: Coverage for statements (45%) does not meet threshold (95%) for lib/source-map-from-file.js
ERROR: Coverage for lines (100%) does not meet threshold (101%) for test/fixtures/async.js
Expand All @@ -223,19 +242,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 (77.22%) does not meet global threshold (100%)
",,ERROR: Coverage for lines (77.4%) does not meet global threshold (100%)
ERROR: Coverage for functions (66.66%) does not meet global threshold (100%)
ERROR: Coverage for branches (62.35%) does not meet global threshold (100%)
ERROR: Coverage for statements (77.22%) does not meet global threshold (100%)
ERROR: Coverage for statements (77.4%) 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 (73.37%) does not meet global threshold (101%)
",,ERROR: Coverage for lines (73.6%) does not meet global threshold (101%)
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
ERROR: Coverage for statements (73.37%) does not meet global threshold (95%)
ERROR: Coverage for statements (73.6%) does not meet global threshold (95%)
"
`;

Expand Down Expand Up @@ -318,17 +337,17 @@ exports[`c8 report generates report from existing temporary files 1`] = `
",--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 73.37 | 59.03 | 62.5 | 73.37 |
All files | 73.6 | 59.03 | 62.5 | 73.6 |
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 | 77.88 | 54.38 | 72 | 77.88 |
lib | 78.19 | 54.38 | 72 | 78.19 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 97.1 | 58.33 | 100 | 97.1 | 148-149,170-171,184-185
report.js | 75.31 | 58.33 | 78.57 | 75.31 | ...249,276-277,283-285,306-311
parse-args.js | 97.19 | 58.33 | 100 | 97.19 | 155-156,177-178,191-192
report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 41.28 | 66.66 | 16.66 | 41.28 |
check-coverage.js | 18.84 | 100 | 0 | 18.84 | 9-11,14-35,38-52,54-69
report.js | 80 | 62.5 | 50 | 80 | 9-10,15-20
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70
report.js | 80.48 | 62.5 | 50 | 80.48 | 9-10,15-20
test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
Expand All @@ -340,24 +359,24 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = `
",--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------

This comment has been minimized.

Copy link
@bcoe

bcoe Jan 13, 2022

Owner

I wish I understood why we need these updates for Node 10 (it will be dropped soon, so not the end of the world) but weird that folder isolation works for newer Node versions.

All files | 73.37 | 59.03 | 62.5 | 73.37 |
All files | 73.6 | 59.03 | 62.5 | 73.6 |
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 | 77.88 | 54.38 | 72 | 77.88 |
lib | 78.19 | 54.38 | 72 | 78.19 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 97.1 | 58.33 | 100 | 97.1 | 148-149,170-171,184-185
report.js | 75.31 | 58.33 | 78.57 | 75.31 | ...249,276-277,283-285,306-311
parse-args.js | 97.19 | 58.33 | 100 | 97.19 | 155-156,177-178,191-192
report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 41.28 | 66.66 | 16.66 | 41.28 |
check-coverage.js | 18.84 | 100 | 0 | 18.84 | 9-11,14-35,38-52,54-69
report.js | 80 | 62.5 | 50 | 80 | 9-10,15-20
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70
report.js | 80.48 | 62.5 | 50 | 80.48 | 9-10,15-20
test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
--------------------------|---------|----------|---------|---------|--------------------------------
,ERROR: Coverage for lines (73.37%) does not meet global threshold (101%)
,ERROR: Coverage for lines (73.6%) does not meet global threshold (101%)
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
ERROR: Coverage for statements (73.37%) does not meet global threshold (95%)
ERROR: Coverage for statements (73.6%) does not meet global threshold (95%)
"
`;
Expand Down

0 comments on commit ff01cd8

Please sign in to comment.