Skip to content

Commit ff01cd8

Browse files
authored
feat: add --extension option (#331)
1 parent 8d6e64c commit ff01cd8

File tree

11 files changed

+129
-38
lines changed

11 files changed

+129
-38
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ Here is a list of common options. Run `c8 --help` for the full list and document
3535
| `--all` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `boolean` | `false` |
3636
| `--src` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[process.cwd()]`|
3737
| `-n`, `--include` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[]` (include all files) |
38-
| `-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) |
38+
| `-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) |
39+
| `-e`, `--extension` | only files matching these extensions will show coverage | `string | Array<string>` | [list](https://github.com/istanbuljs/schema/blob/master/default-extension.js) |
3940
| `--skip-full` | do not show files with 100% statement, branch, and function coverage | `boolean` | `false` |
4041
| `--check-coverage` | check whether coverage is within thresholds provided | `boolean` | `false` |
4142
| `--temp-directory` | directory V8 coverage data is written to and read from | `string` | `process.env.NODE_V8_COVERAGE` |

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export type Watermark = [number, number];
33
export declare class Report {
44
constructor(opts: {
55
exclude?: string | string[],
6+
extension?: string | string[],
67
excludeAfterRemap?: boolean,
78
include?: string | string[],
89
reporter: string[],

lib/commands/check-coverage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ exports.handler = function (argv) {
2222
const report = Report({
2323
include: argv.include,
2424
exclude: argv.exclude,
25+
extension: argv.extension,
2526
reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter],
2627
reportsDirectory: argv['reports-dir'],
2728
tempDirectory: argv.tempDirectory,

lib/commands/report.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ exports.outputReport = async function (argv) {
2121
const report = Report({
2222
include: argv.include,
2323
exclude: argv.exclude,
24+
extension: argv.extension,
2425
excludeAfterRemap: argv.excludeAfterRemap,
2526
reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter],
2627
reportsDirectory: argv['reports-dir'],

lib/parse-args.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const defaultExclude = require('@istanbuljs/schema/default-exclude')
2+
const defaultExtension = require('@istanbuljs/schema/default-extension')
23
const findUp = require('find-up')
34
const { readFileSync } = require('fs')
45
const Yargs = require('yargs/yargs')
@@ -58,6 +59,12 @@ function buildYargs (withCommands = false) {
5859
group: 'Reporting options',
5960
describe: 'a list of specific files and directories that should be excluded from coverage (glob patterns are supported)'
6061
})
62+
.option('extension', {
63+
alias: 'e',
64+
default: defaultExtension,
65+
group: 'Reporting options',
66+
describe: 'a list of specific file extensions that should be covered'
67+
})
6168
.option('exclude-after-remap', {
6269
alias: 'a',
6370
type: 'boolean',

lib/report.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const debuglog = util.debuglog('c8')
1515
class Report {
1616
constructor ({
1717
exclude,
18+
extension,
1819
excludeAfterRemap,
1920
include,
2021
reporter,
@@ -38,6 +39,7 @@ class Report {
3839
this.exclude = new Exclude({
3940
exclude: exclude,
4041
include: include,
42+
extension: extension,
4143
relativePath: !allowExternal,
4244
excludeNodeModules: excludeNodeModules
4345
})

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/custom-ext.special

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require('./async')
2+
3+
console.info('i am a line of code')
4+
5+
function apple (awesome) {
6+
if (false || true) {
7+
console.info('what')
8+
}
9+
if (true || false) {
10+
console.log('hey')
11+
}
12+
}
13+
14+
function missed () {
15+
16+
}
17+
18+
function missed2 () {
19+
20+
}
21+
22+
apple()
23+
apple()
24+
apple()

test/integration.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,4 +630,20 @@ describe('c8', () => {
630630
output.toString('utf8').should.matchSnapshot()
631631
})
632632
})
633+
634+
describe('--extension', () => {
635+
it('includes coverage when extensions specified', () => {
636+
const { output } = spawnSync(nodePath, [
637+
c8Path,
638+
'--exclude="test/*.js"',
639+
'--extension=.js',
640+
'--extension=.special',
641+
'--temp-directory=tmp/extension',
642+
'--clean=false',
643+
nodePath,
644+
require.resolve('./fixtures/custom-ext.special')
645+
])
646+
output.toString('utf8').should.matchSnapshot()
647+
})
648+
})
633649
})

test/integration.js.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ All files | 100 | 100 | 100 | 100 |
125125
,"
126126
`;
127127

128+
exports[`c8 --extension includes coverage when extensions specified 1`] = `
129+
",hey
130+
i am a line of code
131+
what
132+
hey
133+
what
134+
hey
135+
what
136+
hey
137+
--------------------|---------|----------|---------|---------|-------------------
138+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
139+
--------------------|---------|----------|---------|---------|-------------------
140+
All files | 83.33 | 85.71 | 60 | 83.33 |
141+
async.js | 100 | 100 | 100 | 100 |
142+
custom-ext.special | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
143+
--------------------|---------|----------|---------|---------|-------------------
144+
,"
145+
`;
146+
128147
exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
129148
",bar foo
130149
------------|---------|----------|---------|---------|-------------------

0 commit comments

Comments
 (0)