Skip to content

Commit e597c46

Browse files
authored
feat: Add support for --exclude-node-modules to subcommands. (#1053)
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.
1 parent 8dcf180 commit e597c46

File tree

9 files changed

+156
-14
lines changed

9 files changed

+156
-14
lines changed

lib/commands/check-coverage.js

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ exports.builder = function (yargs) {
1313
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded',
1414
global: false
1515
})
16+
.option('exclude-node-modules', {
17+
default: true,
18+
type: 'boolean',
19+
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
20+
global: false
21+
})
1622
.option('exclude-after-remap', {
1723
default: true,
1824
type: 'boolean',

lib/commands/instrument.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ exports.builder = function (yargs) {
5959
default: testExclude.defaultExclude,
6060
describe: 'a list of specific files and directories that should not be instrumented, glob patterns are supported'
6161
})
62+
.option('exclude-node-modules', {
63+
default: true,
64+
type: 'boolean',
65+
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
66+
global: false
67+
})
6268
.option('es-modules', {
6369
default: true,
6470
type: 'boolean',
@@ -100,20 +106,7 @@ exports.handler = function (argv) {
100106
? './lib/instrumenters/istanbul'
101107
: './lib/instrumenters/noop'
102108

103-
const nyc = new NYC({
104-
instrumenter: argv.instrumenter,
105-
sourceMap: argv.sourceMap,
106-
produceSourceMap: argv.produceSourceMap,
107-
extension: argv.extension,
108-
require: argv.require,
109-
cwd: argv.cwd,
110-
compact: argv.compact,
111-
preserveComments: argv.preserveComments,
112-
include: argv.include,
113-
exclude: argv.exclude,
114-
esModules: argv.esModules,
115-
exitOnError: argv.exitOnError
116-
})
109+
const nyc = new NYC(argv)
117110

118111
nyc.instrumentAllFiles(argv.input, argv.output, err => {
119112
if (err) {

lib/commands/report.js

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ exports.builder = function (yargs) {
3030
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded',
3131
global: false
3232
})
33+
.option('exclude-node-modules', {
34+
default: true,
35+
type: 'boolean',
36+
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
37+
global: false
38+
})
3339
.option('exclude-after-remap', {
3440
default: true,
3541
type: 'boolean',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
!node_modules
2+
node_modules/.cache/

test/fixtures/exclude-node-modules/bin/do-nothing.js

Whitespace-only changes.

test/fixtures/exclude-node-modules/node_modules/@istanbuljs/fake-module-1/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/exclude-node-modules/node_modules/@istanbuljs/fake-module-2/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/nyc-integration.js

+132
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,138 @@ describe('the nyc cli', function () {
17371737
})
17381738
})
17391739
})
1740+
1741+
describe('exclude-node-modules', () => {
1742+
const fixturesENM = path.resolve(__dirname, './fixtures/exclude-node-modules')
1743+
const globalArgs = [
1744+
bin,
1745+
'--all=true',
1746+
'--cache=false',
1747+
'--per-file=true',
1748+
'--exclude-node-modules=false',
1749+
'--include=node_modules/@istanbuljs/fake-module-1/**'
1750+
]
1751+
const spawnOpts = {
1752+
cwd: fixturesENM,
1753+
env: env
1754+
}
1755+
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`
1756+
1757+
it('execute', done => {
1758+
function checkReport (code, stderr, stdout, next) {
1759+
code.should.equal(1)
1760+
stderr.should.equal(noCoverageError)
1761+
stdoutShouldEqual(stdout, `
1762+
----------|----------|----------|----------|----------|-------------------|
1763+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
1764+
----------|----------|----------|----------|----------|-------------------|
1765+
All files | 0 | 100 | 100 | 0 | |
1766+
index.js | 0 | 100 | 100 | 0 | 1 |
1767+
----------|----------|----------|----------|----------|-------------------|`)
1768+
next()
1769+
}
1770+
1771+
function executeMainCommand () {
1772+
const args = [
1773+
...globalArgs,
1774+
'--check-coverage=true',
1775+
process.execPath, './bin/do-nothing.js'
1776+
]
1777+
1778+
const proc = spawn(process.execPath, args, spawnOpts)
1779+
1780+
var stderr = ''
1781+
proc.stderr.on('data', function (chunk) {
1782+
stderr += chunk
1783+
})
1784+
1785+
var stdout = ''
1786+
proc.stdout.on('data', function (chunk) {
1787+
stdout += chunk
1788+
})
1789+
1790+
proc.on('close', code => checkReport(code, stderr, stdout, executeReport))
1791+
}
1792+
1793+
function executeReport () {
1794+
const args = [
1795+
...globalArgs,
1796+
'--check-coverage=true',
1797+
'report'
1798+
]
1799+
1800+
const proc = spawn(process.execPath, args, spawnOpts)
1801+
1802+
var stderr = ''
1803+
proc.stderr.on('data', function (chunk) {
1804+
stderr += chunk
1805+
})
1806+
1807+
var stdout = ''
1808+
proc.stdout.on('data', function (chunk) {
1809+
stdout += chunk
1810+
})
1811+
1812+
proc.on('close', code => checkReport(code, stderr, stdout, executeCheckCoverage))
1813+
}
1814+
1815+
function executeCheckCoverage () {
1816+
const args = [
1817+
...globalArgs,
1818+
'check-coverage'
1819+
]
1820+
1821+
const proc = spawn(process.execPath, args, spawnOpts)
1822+
1823+
var stderr = ''
1824+
proc.stderr.on('data', function (chunk) {
1825+
stderr += chunk
1826+
})
1827+
1828+
var stdout = ''
1829+
proc.stdout.on('data', function (chunk) {
1830+
stdout += chunk
1831+
})
1832+
1833+
proc.on('close', code => {
1834+
code.should.equal(1)
1835+
stderr.should.equal(noCoverageError)
1836+
stdoutShouldEqual(stdout, '')
1837+
done()
1838+
})
1839+
}
1840+
1841+
executeMainCommand()
1842+
})
1843+
1844+
it('instrument', done => {
1845+
const args = [
1846+
...globalArgs,
1847+
'instrument',
1848+
'node_modules'
1849+
]
1850+
1851+
const proc = spawn(process.execPath, args, spawnOpts)
1852+
1853+
var stderr = ''
1854+
proc.stderr.on('data', function (chunk) {
1855+
stderr += chunk
1856+
})
1857+
1858+
var stdout = ''
1859+
proc.stdout.on('data', function (chunk) {
1860+
stdout += chunk
1861+
})
1862+
1863+
proc.on('close', code => {
1864+
code.should.equal(0)
1865+
stderr.should.equal('')
1866+
stdout.should.match(/fake-module-1/)
1867+
stdout.should.not.match(/fake-module-2/)
1868+
done()
1869+
})
1870+
})
1871+
})
17401872
})
17411873

17421874
function stdoutShouldEqual (stdout, expected) {

0 commit comments

Comments
 (0)