From 6716c12f0ec2aa1c4689231b1a65d20099499ca0 Mon Sep 17 00:00:00 2001 From: Eddie Leffler Date: Fri, 24 Aug 2018 15:02:00 -0700 Subject: [PATCH 1/2] Support turning off node_modules default exclude via flag --- index.js | 4 +++- lib/config-util.js | 6 ++++++ test/src/nyc-tap.js | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e7fde82bc..97c120a5e 100755 --- a/index.js +++ b/index.js @@ -56,7 +56,9 @@ function NYC (config) { this.exclude = testExclude({ cwd: this.cwd, include: config.include, - exclude: config.exclude + exclude: config.exclude, + // Make sure this is true unless explicitly set to `false`. `undefined` is still `true`. + excludeNodeModules: config.excludeNodeModules !== false }) this.sourceMaps = new SourceMaps({ diff --git a/lib/config-util.js b/lib/config-util.js index d8b8cf618..038dd271c 100644 --- a/lib/config-util.js +++ b/lib/config-util.js @@ -80,6 +80,12 @@ Config.buildYargs = function (cwd) { description: 'should exclude logic be performed after the source-map remaps filenames?', 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('include', { alias: 'n', default: [], diff --git a/test/src/nyc-tap.js b/test/src/nyc-tap.js index 7fe188124..114c00200 100644 --- a/test/src/nyc-tap.js +++ b/test/src/nyc-tap.js @@ -130,6 +130,20 @@ describe('nyc', function () { nyc.exclude.shouldInstrument('/cwd/foo/bar/__tests__/foo.js', './__tests__/foo.js').should.equal(false) }) + it('should allow turning off default node_modules exclude', function () { + var nyc = new NYC(configUtil.buildYargs('/cwd').parse([ + '--exclude-node-modules', 'false', + '--exclude=**/__tests__/**', + '--exclude=node_modules/**' + ])) + // For this test, only excluding root node_modules. Local node_modules are allowed, but we + // still exclude matching items inside of local node_modules. + nyc.exclude.shouldInstrument('/cwd/foo', 'foo').should.equal(true) + nyc.exclude.shouldInstrument('/cwd/node_modules/bar', 'node_modules/bar').should.equal(false) + nyc.exclude.shouldInstrument('/cwd/foo/node_modules/bar', 'foo/node_modules/bar').should.equal(true) + nyc.exclude.shouldInstrument('/cwd/foo/node_modules/bar/__tests__/baz.js', 'foo/node_modules/bar/__tests__/baz.js').should.equal(false) + }) + it('should exclude appropriately with config.exclude', function () { var nyc = new NYC(configUtil.buildYargs(fixtures).parse()) From 122e9d9c2196eea1a57c7dd28d1b6a7798a6f04c Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Tue, 2 Apr 2019 07:46:31 -0400 Subject: [PATCH 2/2] Fix error introduced by when resolving conflict It's possible for `config.excludeNodeModules` to be undefined, this is viewed by testExclude as 'not true'. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index a2e17cc7e..638b50015 100755 --- a/index.js +++ b/index.js @@ -58,7 +58,7 @@ function NYC (config) { cwd: this.cwd, include: config.include, exclude: config.exclude, - excludeNodeModules: config.excludeNodeModules, + excludeNodeModules: config.excludeNodeModules !== false, extension: this.extensions })