Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for --exclude-node-modules to subcommands. #1053

Merged
merged 1 commit into from Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/commands/check-coverage.js
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
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({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little weird that this refactor happens in this pull, seems like an improvement to picking and choosing parameters though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JaKXz saw that I was manually adding yet another option to this constructor, asked if we could do this.

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
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
@@ -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
@@ -0,0 +1 @@
{}
132 changes: 132 additions & 0 deletions test/nyc-integration.js
Expand Up @@ -1731,6 +1731,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