diff --git a/lib/commands/check-coverage.js b/lib/commands/check-coverage.js index c302ee916..8eb36eb50 100644 --- a/lib/commands/check-coverage.js +++ b/lib/commands/check-coverage.js @@ -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', diff --git a/lib/commands/instrument.js b/lib/commands/instrument.js index 3c0fe9d3b..c22475c24 100644 --- a/lib/commands/instrument.js +++ b/lib/commands/instrument.js @@ -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', @@ -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) { diff --git a/lib/commands/report.js b/lib/commands/report.js index 8dfc0f4ab..739ff30dc 100644 --- a/lib/commands/report.js +++ b/lib/commands/report.js @@ -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', diff --git a/test/fixtures/exclude-node-modules/.gitignore b/test/fixtures/exclude-node-modules/.gitignore new file mode 100644 index 000000000..9ef48a5db --- /dev/null +++ b/test/fixtures/exclude-node-modules/.gitignore @@ -0,0 +1,2 @@ +!node_modules +node_modules/.cache/ diff --git a/test/fixtures/exclude-node-modules/bin/do-nothing.js b/test/fixtures/exclude-node-modules/bin/do-nothing.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/exclude-node-modules/node_modules/@istanbuljs/fake-module-1/index.js b/test/fixtures/exclude-node-modules/node_modules/@istanbuljs/fake-module-1/index.js new file mode 100644 index 000000000..6be02374d --- /dev/null +++ b/test/fixtures/exclude-node-modules/node_modules/@istanbuljs/fake-module-1/index.js @@ -0,0 +1 @@ +console.log('hello world'); diff --git a/test/fixtures/exclude-node-modules/node_modules/@istanbuljs/fake-module-2/index.js b/test/fixtures/exclude-node-modules/node_modules/@istanbuljs/fake-module-2/index.js new file mode 100644 index 000000000..6be02374d --- /dev/null +++ b/test/fixtures/exclude-node-modules/node_modules/@istanbuljs/fake-module-2/index.js @@ -0,0 +1 @@ +console.log('hello world'); diff --git a/test/fixtures/exclude-node-modules/package.json b/test/fixtures/exclude-node-modules/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/fixtures/exclude-node-modules/package.json @@ -0,0 +1 @@ +{} diff --git a/test/nyc-integration.js b/test/nyc-integration.js index 53527ffd8..88155a667 100644 --- a/test/nyc-integration.js +++ b/test/nyc-integration.js @@ -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) {