Skip to content

Commit

Permalink
feat: Add support for --exclude-node-modules to subcommands. (#1053)
Browse files Browse the repository at this point in the history
This adds support for the `--exclude-node-modules` option/setting to
instrument, check-coverage and report sub-commands.

Add testing to verify that `--exclude-node-modules=false` is honored for
all commands.
  • Loading branch information
coreyfarrell authored Apr 6, 2019
1 parent 8dcf180 commit e597c46
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 14 deletions.
6 changes: 6 additions & 0 deletions lib/commands/check-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ exports.builder = function (yargs) {
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded',
global: false
})
.option('exclude-node-modules', {
default: true,
type: 'boolean',
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
global: false
})
.option('exclude-after-remap', {
default: true,
type: 'boolean',
Expand Down
21 changes: 7 additions & 14 deletions lib/commands/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ exports.builder = function (yargs) {
default: testExclude.defaultExclude,
describe: 'a list of specific files and directories that should not be instrumented, glob patterns are supported'
})
.option('exclude-node-modules', {
default: true,
type: 'boolean',
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
global: false
})
.option('es-modules', {
default: true,
type: 'boolean',
Expand Down Expand Up @@ -100,20 +106,7 @@ exports.handler = function (argv) {
? './lib/instrumenters/istanbul'
: './lib/instrumenters/noop'

const nyc = new NYC({
instrumenter: argv.instrumenter,
sourceMap: argv.sourceMap,
produceSourceMap: argv.produceSourceMap,
extension: argv.extension,
require: argv.require,
cwd: argv.cwd,
compact: argv.compact,
preserveComments: argv.preserveComments,
include: argv.include,
exclude: argv.exclude,
esModules: argv.esModules,
exitOnError: argv.exitOnError
})
const nyc = new NYC(argv)

nyc.instrumentAllFiles(argv.input, argv.output, err => {
if (err) {
Expand Down
6 changes: 6 additions & 0 deletions lib/commands/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ exports.builder = function (yargs) {
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded',
global: false
})
.option('exclude-node-modules', {
default: true,
type: 'boolean',
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
global: false
})
.option('exclude-after-remap', {
default: true,
type: 'boolean',
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/exclude-node-modules/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!node_modules
node_modules/.cache/
Empty file.

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

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

1 change: 1 addition & 0 deletions test/fixtures/exclude-node-modules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
132 changes: 132 additions & 0 deletions test/nyc-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,138 @@ describe('the nyc cli', function () {
})
})
})

describe('exclude-node-modules', () => {
const fixturesENM = path.resolve(__dirname, './fixtures/exclude-node-modules')
const globalArgs = [
bin,
'--all=true',
'--cache=false',
'--per-file=true',
'--exclude-node-modules=false',
'--include=node_modules/@istanbuljs/fake-module-1/**'
]
const spawnOpts = {
cwd: fixturesENM,
env: env
}
const noCoverageError = `ERROR: Coverage for lines (0%) does not meet threshold (90%) for ${path.join(fixturesENM, 'node_modules/@istanbuljs/fake-module-1/index.js')}\n`

it('execute', done => {
function checkReport (code, stderr, stdout, next) {
code.should.equal(1)
stderr.should.equal(noCoverageError)
stdoutShouldEqual(stdout, `
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 0 | 100 | 100 | 0 | |
index.js | 0 | 100 | 100 | 0 | 1 |
----------|----------|----------|----------|----------|-------------------|`)
next()
}

function executeMainCommand () {
const args = [
...globalArgs,
'--check-coverage=true',
process.execPath, './bin/do-nothing.js'
]

const proc = spawn(process.execPath, args, spawnOpts)

var stderr = ''
proc.stderr.on('data', function (chunk) {
stderr += chunk
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', code => checkReport(code, stderr, stdout, executeReport))
}

function executeReport () {
const args = [
...globalArgs,
'--check-coverage=true',
'report'
]

const proc = spawn(process.execPath, args, spawnOpts)

var stderr = ''
proc.stderr.on('data', function (chunk) {
stderr += chunk
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', code => checkReport(code, stderr, stdout, executeCheckCoverage))
}

function executeCheckCoverage () {
const args = [
...globalArgs,
'check-coverage'
]

const proc = spawn(process.execPath, args, spawnOpts)

var stderr = ''
proc.stderr.on('data', function (chunk) {
stderr += chunk
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', code => {
code.should.equal(1)
stderr.should.equal(noCoverageError)
stdoutShouldEqual(stdout, '')
done()
})
}

executeMainCommand()
})

it('instrument', done => {
const args = [
...globalArgs,
'instrument',
'node_modules'
]

const proc = spawn(process.execPath, args, spawnOpts)

var stderr = ''
proc.stderr.on('data', function (chunk) {
stderr += chunk
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', code => {
code.should.equal(0)
stderr.should.equal('')
stdout.should.match(/fake-module-1/)
stdout.should.not.match(/fake-module-2/)
done()
})
})
})
})

function stdoutShouldEqual (stdout, expected) {
Expand Down

0 comments on commit e597c46

Please sign in to comment.