diff --git a/bin/nyc.js b/bin/nyc.js index 841205a21..6dd78bf46 100755 --- a/bin/nyc.js +++ b/bin/nyc.js @@ -4,7 +4,6 @@ const configUtil = require('../lib/config-util') const foreground = require('foreground-child') const NYC = require('../index.js') -const processArgs = require('../lib/process-args') const sw = require('spawn-wrap') const wrapper = require.resolve('./wrap.js') @@ -13,13 +12,10 @@ const wrapper = require.resolve('./wrap.js') // we keep these values in a few different forms, // used in the various execution contexts of nyc: // reporting, instrumenting subprocesses, etc. -const yargs = configUtil.buildYargs() -const instrumenterArgs = processArgs.hideInstrumenteeArgs() -const config = configUtil.loadConfig(yargs.parse(instrumenterArgs)) -configUtil.addCommandsAndHelp(yargs) -const argv = yargs.config(config).parse(instrumenterArgs) async function main () { + const { argv, childArgs, yargs } = await configUtil() + if (['check-coverage', 'report', 'instrument', 'merge'].includes(argv._[0])) { // look in lib/commands for logic. return @@ -68,11 +64,7 @@ async function main () { // set process.exitCode. Keep track so that both children are run, but // a non-zero exit codes in either one leads to an overall non-zero exit code. process.exitCode = 0 - foreground(processArgs.hideInstrumenterArgs( - // use the same argv description, but don't exit - // for flags like --help. - configUtil.buildYargs().parse(process.argv.slice(2)) - ), async () => { + foreground(childArgs, async () => { var mainChildExitCode = process.exitCode await nyc.writeProcessIndex() diff --git a/index.js b/index.js index 6df5dd03c..8b45915b1 100755 --- a/index.js +++ b/index.js @@ -492,15 +492,8 @@ class NYC { eachReport (filenames, iterator, baseDirectory) { baseDirectory = baseDirectory || this.tempDirectory() - if (typeof filenames === 'function') { - iterator = filenames - filenames = undefined - } - - var _this = this - var files = filenames || fs.readdirSync(baseDirectory) - - files.forEach(function (f) { + const files = filenames || fs.readdirSync(baseDirectory) + files.forEach(f => { var report try { report = JSON.parse(fs.readFileSync( @@ -508,7 +501,7 @@ class NYC { 'utf-8' )) - _this.sourceMaps.reloadCachedSourceMapsSync(report) + this.sourceMaps.reloadCachedSourceMapsSync(report) } catch (e) { // handle corrupt JSON output. report = {} } diff --git a/lib/config-util.js b/lib/config-util.js index 177469b60..3acf94a6d 100644 --- a/lib/config-util.js +++ b/lib/config-util.js @@ -1,61 +1,26 @@ 'use strict' -const fs = require('fs') const path = require('path') const findUp = require('find-up') const testExclude = require('test-exclude') const Yargs = require('yargs/yargs') -var Config = {} +const processArgs = require('./process-args') +const { loadNycConfig } = require('@istanbuljs/load-nyc-config') -function guessCWD (cwd) { +async function guessCWD (cwd) { cwd = cwd || process.env.NYC_CWD || process.cwd() - const pkgPath = findUp.sync('package.json', { cwd: cwd }) + const pkgPath = await findUp('package.json', { cwd }) if (pkgPath) { cwd = path.dirname(pkgPath) } - return cwd -} - -Config.loadConfig = function (argv, cwd) { - const rcOptions = [ - argv.nycrcPath || '.nycrc', - '.nycrc.json', - '.nycrc.yml', - '.nycrc.yaml', - 'nyc.config.js' - ] - const rcPath = findUp.sync(rcOptions, { cwd: guessCWD(cwd) }) - let config = {} - - if (rcPath) { - const rcExt = path.extname(rcPath.toLowerCase()) - if (rcExt === '.js') { - config = require(rcPath) - } else if (rcExt === '.yml' || rcExt === '.yaml') { - config = require('js-yaml').load( - fs.readFileSync(rcPath, 'utf8') - ) - } else { - config = JSON.parse( - fs.readFileSync(rcPath, 'utf-8') - ) - } - } - - if (config.require) config.require = [].concat(config.require) - if (config.extension) config.extension = [].concat(config.extension) - if (config.exclude) config.exclude = [].concat(config.exclude) - if (config.include) config.include = [].concat(config.include) - return config + return cwd } -// build a yargs object, omitting any settings -// that would cause the application to exit early. -Config.buildYargs = function (cwd) { - cwd = guessCWD(cwd) - return Yargs([]) +async function processConfig (cwd) { + cwd = await guessCWD(cwd) + const yargs = Yargs([]) .usage('$0 [command] [options]') .usage('$0 [options] [bin-to-instrument]') .option('reporter', { @@ -243,7 +208,6 @@ Config.buildYargs = function (cwd) { global: false }) .option('nycrc-path', { - default: '.nycrc', description: 'specify a different .nycrc path', global: false }) @@ -269,7 +233,6 @@ Config.buildYargs = function (cwd) { type: 'boolean', global: false }) - .pkgConf('nyc', cwd) .example('$0 npm test', 'instrument your tests with coverage') .example('$0 --require @babel/register npm test', 'instrument your tests with coverage and transpile with Babel') .example('$0 report --reporter=text-lcov', 'output lcov report after running your tests') @@ -278,20 +241,30 @@ Config.buildYargs = function (cwd) { .boolean('version') .help(false) .version(false) -} -// we add operations that would make yargs -// exit post-hoc, allowing for a multi-pass -// parsing step. -Config.addCommandsAndHelp = function (yargs) { - return yargs + const instrumenterArgs = processArgs.hideInstrumenteeArgs() + + // This yargs.parse must come before any options that exit post-hoc + const childArgs = processArgs.hideInstrumenterArgs(yargs.parse(process.argv.slice(2))) + const config = await loadNycConfig(yargs.parse(instrumenterArgs)) + + yargs + .config(config) .help('h') .alias('h', 'help') .version() - .command(require('../lib/commands/check-coverage')) - .command(require('../lib/commands/instrument')) - .command(require('../lib/commands/report')) - .command(require('../lib/commands/merge')) + .command(require('./commands/check-coverage')) + .command(require('./commands/instrument')) + .command(require('./commands/report')) + .command(require('./commands/merge')) + + return { + get argv () { + return yargs.parse(instrumenterArgs) + }, + childArgs, + yargs + } } -module.exports = Config +module.exports = processConfig diff --git a/package-lock.json b/package-lock.json index 9b6b38de9..df614afa4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -109,6 +109,17 @@ "to-fast-properties": "^2.0.0" } }, + "@istanbuljs/load-nyc-config": { + "version": "1.0.0-alpha.0", + "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.0.0-alpha.0.tgz", + "integrity": "sha512-tDRosscSl5tXCN2rYsfRddj/sl8ApLm30ByOhGFpMtCzjVbunPTwwexHFjxcPycSN6pUunUhOW+AcTfb9MB+gA==", + "requires": { + "camelcase": "^5.3.1", + "find-up": "^4.1.0", + "js-yaml": "^3.13.1", + "resolve-from": "^5.0.0" + } + }, "@istanbuljs/schema": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/schema/-/schema-0.1.0.tgz", diff --git a/package.json b/package.json index 44eb7a48a..7e5de9d8e 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "author": "Ben Coe ", "license": "ISC", "dependencies": { + "@istanbuljs/load-nyc-config": "^1.0.0-alpha.0", "caching-transform": "^4.0.0", "convert-source-map": "^1.6.0", "cp-file": "^7.0.0", diff --git a/tap-snapshots/test-nyc-integration.js-TAP.test.js b/tap-snapshots/test-nyc-integration.js-TAP.test.js index 41f257465..83d458618 100644 --- a/tap-snapshots/test-nyc-integration.js-TAP.test.js +++ b/tap-snapshots/test-nyc-integration.js-TAP.test.js @@ -16,6 +16,21 @@ All files | 44.44 | 100 | 33.33 | 44.44 | ` +exports[`test/nyc-integration.js TAP --all instruments unknown extensions as js > stdout 1`] = ` +run +------------------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +------------------------|---------|----------|---------|---------|------------------- +All files | 11.11 | 100 | 0 | 11.11 | + check-instrumented.es6 | 0 | 100 | 0 | 0 | 5,6 + check-instrumented.js | 0 | 100 | 0 | 0 | 5,6 + not-loaded.es6 | 0 | 100 | 100 | 0 | 1,2 + not-loaded.js | 0 | 100 | 100 | 0 | 1,2 + run.js | 100 | 100 | 100 | 100 | +------------------------|---------|----------|---------|---------|------------------- + +` + exports[`test/nyc-integration.js TAP --all uses source-maps to exclude original sources from reports > stdout 1`] = ` ----------|---------|----------|---------|---------|------------------- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s @@ -838,6 +853,30 @@ exports[`test/nyc-integration.js TAP passes configuration via environment variab ] ` +exports[`test/nyc-integration.js TAP produce-source-map enabled > stdout 1`] = ` +Error: Blarrh + at blah (./stack-trace.js:3:1) +----------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +----------------|---------|----------|---------|---------|------------------- +All files | 100 | 100 | 100 | 100 | + stack-trace.js | 100 | 100 | 100 | 100 | +----------------|---------|----------|---------|---------|------------------- + +` + +exports[`test/nyc-integration.js TAP produce-source-map not enabled > stdout 1`] = ` +Error: Blarrh + at blah (./stack-trace.js:1:1037) +----------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +----------------|---------|----------|---------|---------|------------------- +All files | 100 | 100 | 100 | 100 | + stack-trace.js | 100 | 100 | 100 | 100 | +----------------|---------|----------|---------|---------|------------------- + +` + exports[`test/nyc-integration.js TAP recursive run does not throw > stdout 1`] = ` ` diff --git a/test/add-all-files.js b/test/add-all-files.js index eab3e58a2..38750dc7f 100644 --- a/test/add-all-files.js +++ b/test/add-all-files.js @@ -7,9 +7,8 @@ const t = require('tap') const ap = require('any-path') const NYC = require('../self-coverage') -const configUtil = require('../self-coverage/lib/config-util') -const resetState = require('./helpers/reset-state') +const { parseArgv, resetState } = require('./helpers') const fixtures = path.resolve(__dirname, 'fixtures') const transpileHook = path.resolve(__dirname, 'fixtures/transpile-hook') @@ -17,7 +16,7 @@ const transpileHook = path.resolve(__dirname, 'fixtures/transpile-hook') t.beforeEach(resetState) t.test('outputs an empty coverage report for all files that are not excluded', async t => { - const nyc = new NYC(configUtil.buildYargs(fixtures).parse()) + const nyc = new NYC(await parseArgv(fixtures)) await nyc.reset() await nyc.addAllFiles() @@ -32,7 +31,7 @@ t.test('outputs an empty coverage report for all files that are not excluded', a t.test('outputs an empty coverage report for multiple configured extensions', async t => { const cwd = path.resolve(fixtures, './conf-multiple-extensions') - const nyc = new NYC(configUtil.buildYargs(cwd).parse()) + const nyc = new NYC(await parseArgv(cwd)) await nyc.reset() await nyc.addAllFiles() @@ -61,7 +60,7 @@ t.test('transpiles .js files added via addAllFiles', async t => { 'utf-8' ) - const nyc = new NYC(configUtil.buildYargs(fixtures).parse(['--require', transpileHook])) + const nyc = new NYC(await parseArgv(fixtures, ['--require', transpileHook])) await nyc.reset() await nyc.addAllFiles() @@ -83,7 +82,7 @@ t.test('does not attempt to transpile files when they are excluded', async t => 'utf-8' ) - const nyc = new NYC(configUtil.buildYargs(fixtures).parse([ + const nyc = new NYC(await parseArgv(fixtures, [ `--require=${transpileHook}`, '--extension=.do-not-transpile', '--include=needs-transpile.do-not-transpile' @@ -103,10 +102,10 @@ t.test('transpiles non-.js files added via addAllFiles', async t => { 'utf-8' ) - const nyc = (new NYC(configUtil.buildYargs(fixtures).parse([ + const nyc = new NYC(await parseArgv(fixtures, [ `--require=${transpileHook}`, '--extension=.whatever' - ]))) + ])) await nyc.reset() await nyc.addAllFiles() diff --git a/test/cache.js b/test/cache.js index b2d7c982c..f8176fa87 100644 --- a/test/cache.js +++ b/test/cache.js @@ -7,16 +7,15 @@ const t = require('tap') const rimraf = promisify(require('rimraf')) const NYC = require('../self-coverage') -const configUtil = require('../self-coverage/lib/config-util') -const { resetState, runNYC } = require('./helpers') +const { parseArgv, resetState, runNYC } = require('./helpers') const fixtures = path.resolve(__dirname, './fixtures') t.beforeEach(resetState) async function cacheTest (t, script) { - const nyc = new NYC(configUtil.buildYargs(fixtures).parse()) + const nyc = new NYC(await parseArgv(fixtures)) await rimraf(nyc.cacheDirectory) const { status } = await runNYC({ diff --git a/test/config.js b/test/config.js index 9163d8cc3..62b440c87 100644 --- a/test/config.js +++ b/test/config.js @@ -5,22 +5,23 @@ const path = require('path') const { test } = require('tap') const NYC = require('../self-coverage') -const configUtil = require('../self-coverage/lib/config-util') + +const { parseArgv } = require('./helpers') test("loads 'exclude' patterns from package.json#nyc", async t => { - const nyc = new NYC(configUtil.buildYargs(path.resolve(__dirname, './fixtures')).parse()) + const nyc = new NYC(await parseArgv(path.resolve(__dirname, './fixtures'))) t.strictEqual(nyc.exclude.exclude.length, 8) }) test("loads 'extension' patterns from package.json#nyc", async t => { - const nyc = new NYC(configUtil.buildYargs(path.resolve(__dirname, './fixtures/conf-multiple-extensions')).parse()) + const nyc = new NYC(await parseArgv(path.resolve(__dirname, './fixtures/conf-multiple-extensions'))) t.strictEqual(nyc.extensions.length, 3) }) test("ignores 'include' option if it's falsy or []", async t => { - const nyc1 = new NYC(configUtil.buildYargs(path.resolve(__dirname, './fixtures/conf-empty')).parse()) + const nyc1 = new NYC(await parseArgv(path.resolve(__dirname, './fixtures/conf-empty'))) t.strictEqual(nyc1.exclude.include, false) @@ -32,7 +33,7 @@ test("ignores 'include' option if it's falsy or []", async t => { }) test("ignores 'exclude' option if it's falsy", async t => { - const nyc = new NYC(configUtil.buildYargs(path.resolve(__dirname, './fixtures/conf-empty')).parse()) + const nyc = new NYC(await parseArgv(path.resolve(__dirname, './fixtures/conf-empty'))) t.strictEqual(nyc.exclude.exclude.length, 15) }) diff --git a/test/cwd.js b/test/cwd.js index b62681cc1..b079bf034 100644 --- a/test/cwd.js +++ b/test/cwd.js @@ -5,14 +5,15 @@ const path = require('path') const t = require('tap') const NYC = require('../self-coverage') -const configUtil = require('../self-coverage/lib/config-util') + +const { parseArgv } = require('./helpers') t.beforeEach(async () => { delete process.env.NYC_CWD }) t.test('sets cwd to process.cwd() if no environment variable is set', async t => { - const nyc = new NYC(configUtil.buildYargs().parse()) + const nyc = new NYC(await parseArgv()) t.strictEqual(nyc.cwd, process.cwd()) }) @@ -20,19 +21,19 @@ t.test('sets cwd to process.cwd() if no environment variable is set', async t => t.test('uses NYC_CWD environment variable for cwd if it is set', async t => { const fixtures = path.resolve(__dirname, './fixtures') process.env.NYC_CWD = fixtures - const nyc = new NYC(configUtil.buildYargs().parse()) + const nyc = new NYC(await parseArgv()) t.strictEqual(nyc.cwd, fixtures) }) t.test('will look upwards for package.json from cwd', async t => { - const nyc = new NYC(configUtil.buildYargs(__dirname).parse()) + const nyc = new NYC(await parseArgv(__dirname)) t.strictEqual(nyc.cwd, path.join(__dirname, '..')) }) t.test('uses --cwd for cwd if it is set (highest priority and does not look upwards for package.json) ', async t => { - const nyc = new NYC(configUtil.buildYargs(__dirname).parse(['--cwd', __dirname])) + const nyc = new NYC(await parseArgv(__dirname, ['--cwd', __dirname])) t.strictEqual(nyc.cwd, __dirname) }) diff --git a/test/fixtures/conf-multiple-extensions/run.js b/test/fixtures/conf-multiple-extensions/run.js new file mode 100644 index 000000000..b63af05a6 --- /dev/null +++ b/test/fixtures/conf-multiple-extensions/run.js @@ -0,0 +1,4 @@ +#!/usr/bin/env node +'use strict'; + +console.log('run'); diff --git a/test/helpers/index.js b/test/helpers/index.js index b5c3001a6..4041a0fe5 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -11,5 +11,6 @@ module.exports = { testFailure: require('./test-failure'), runNYC: require('./run-nyc'), tempDirSetup: require('./temp-dir-setup'), - envCheckConfig: require('./env-check-config') + envCheckConfig: require('./env-check-config'), + parseArgv: require('./parse-argv') } diff --git a/test/helpers/parse-argv.js b/test/helpers/parse-argv.js new file mode 100644 index 000000000..2e3c12de5 --- /dev/null +++ b/test/helpers/parse-argv.js @@ -0,0 +1,9 @@ +const configUtil = require('../../self-coverage/lib/config-util') + +async function parseArgv (cwd, argv) { + const { yargs } = await configUtil(cwd) + + return yargs.parse(argv) +} + +module.exports = parseArgv diff --git a/test/helpers/source-map-support.js b/test/helpers/source-map-support.js new file mode 100644 index 000000000..43ad83aeb --- /dev/null +++ b/test/helpers/source-map-support.js @@ -0,0 +1,3 @@ +'use strict' + +require('source-map-support').install({ hookRequire: true }) diff --git a/test/nyc-integration.js b/test/nyc-integration.js index 9e4a6daa7..ba68d2280 100644 --- a/test/nyc-integration.js +++ b/test/nyc-integration.js @@ -586,3 +586,8 @@ t.test('reports error if input directory is missing', t => testFailure(t, { t.test('reports error if input is not a directory', t => testFailure(t, { args: ['merge', './package.json'] })) + +t.test('--all instruments unknown extensions as js', t => testSuccess(t, { + cwd: path.resolve(fixturesCLI, '../conf-multiple-extensions'), + args: ['--all', process.execPath, './run.js'] +})) diff --git a/test/report.js b/test/report.js index 5668cbb12..645cbb14b 100644 --- a/test/report.js +++ b/test/report.js @@ -9,9 +9,8 @@ const isWindows = require('is-windows')() const rimraf = promisify(require('rimraf')) const NYC = require('../self-coverage') -const configUtil = require('../self-coverage/lib/config-util') -const { runNYC, resetState } = require('./helpers') +const { parseArgv, runNYC, resetState } = require('./helpers') const fixtures = path.resolve(__dirname, 'fixtures') @@ -24,7 +23,7 @@ async function testSignal (t, signal) { return } - const nyc = new NYC(configUtil.buildYargs(fixtures).parse()) + const nyc = new NYC(await parseArgv(fixtures)) await runNYC({ args: [`./${signal}.js`], cwd: fixtures @@ -41,9 +40,10 @@ t.test('writes coverage report when process is killed with SIGTERM', t => testSi t.test('writes coverage report when process is killed with SIGINT', t => testSignal(t, 'sigint')) t.test('allows coverage report to be output in an alternative directory', async t => { - const nyc = new NYC(configUtil.buildYargs().parse( - ['--report-dir=./alternative-report', '--reporter=lcov'] - )) + const nyc = new NYC(await parseArgv(undefined, [ + '--report-dir=./alternative-report', + '--reporter=lcov' + ])) await nyc.reset() await nyc.report() diff --git a/test/should-instrument.js b/test/should-instrument.js index 81b612914..8afc3d858 100644 --- a/test/should-instrument.js +++ b/test/should-instrument.js @@ -1,13 +1,15 @@ const path = require('path') const NYC = require('../self-coverage') -const configUtil = require('../self-coverage/lib/config-util') + +const { parseArgv } = require('./helpers') + const fixtures = path.resolve(__dirname, './fixtures') const t = require('tap') const rootDir = path.resolve('/') -t.test('should exclude appropriately with defaults', t => { - const nyc = new NYC(configUtil.buildYargs(rootDir).parse([ +t.test('should exclude appropriately with defaults', async t => { + const nyc = new NYC(await parseArgv(rootDir, [ '--exclude=test/**', '--exclude=test{,-*}.js', '--exclude=**/*.test.js', @@ -26,11 +28,10 @@ t.test('should exclude appropriately with defaults', t => { t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo/bar/test.js'), '.\\test.js')) t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo/bar/foo.test.js'), './foo.test.js')) t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo/bar/__tests__/foo.js'), './__tests__/foo.js')) - t.done() }) -t.test('should exclude appropriately with config.exclude', t => { - const nyc = new NYC(configUtil.buildYargs(fixtures).parse()) +t.test('should exclude appropriately with config.exclude', async t => { + const nyc = new NYC(await parseArgv(fixtures)) // fixtures/package.json configures excludes: "blarg", "blerg" t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'blarg.js'), 'blarg.js')) @@ -38,33 +39,29 @@ t.test('should exclude appropriately with config.exclude', t => { t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'blerg.js'), 'blerg.js')) t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'blerg.js'), './blerg.js')) t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'blerg.js'), '.\\blerg.js')) - t.done() }) -t.test('should exclude outside of the current working directory', t => { - const nyc = new NYC(configUtil.buildYargs(path.join(rootDir, 'foo')).parse()) +t.test('should exclude outside of the current working directory', async t => { + const nyc = new NYC(await parseArgv(path.join(rootDir, 'foo'))) t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'bar.js'), '../bar.js')) - t.done() }) -t.test('should not exclude if the current working directory is inside node_modules', t => { +t.test('should not exclude if the current working directory is inside node_modules', async t => { const cwd = path.join(rootDir, 'node_modules', 'foo') - const nyc = new NYC(configUtil.buildYargs(cwd).parse()) + const nyc = new NYC(await parseArgv(cwd)) t.true(nyc.exclude.shouldInstrument(path.join(cwd, 'bar.js'), './bar.js')) t.true(nyc.exclude.shouldInstrument(path.join(cwd, 'bar.js'), '.\\bar.js')) - t.done() }) -t.test('allows files to be explicitly included, rather than excluded', t => { - const nyc = new NYC(configUtil.buildYargs(rootDir).parse(['--include=foo.js'])) +t.test('allows files to be explicitly included, rather than excluded', async t => { + const nyc = new NYC(await parseArgv(rootDir, ['--include=foo.js'])) t.true(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo.js'), 'foo.js')) t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'index.js'), 'index.js')) - t.done() }) -t.test('exclude overrides include', t => { - const nyc = new NYC(configUtil.buildYargs(rootDir).parse([ +t.test('exclude overrides include', async t => { + const nyc = new NYC(await parseArgv(rootDir, [ '--include=foo.js', '--include=test.js', '--exclude=**/node_modules/**', @@ -74,5 +71,4 @@ t.test('exclude overrides include', t => { t.true(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo.js'), 'foo.js')) t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'test.js'), 'test.js')) - t.done() }) diff --git a/test/source-map-support.js b/test/source-map-support.js index a68f95cd1..3f2ac11b5 100644 --- a/test/source-map-support.js +++ b/test/source-map-support.js @@ -3,9 +3,8 @@ const t = require('tap') const NYC = require('../self-coverage') -const configUtil = require('../self-coverage/lib/config-util') -const resetState = require('./helpers/reset-state') +const { parseArgv, resetState } = require('./helpers') // we test exit handlers in nyc-integration.js. NYC.prototype._wrapExit = () => {} @@ -15,7 +14,7 @@ require('source-map-support').install({ hookRequire: true }) t.beforeEach(resetState) t.test('handles stack traces', async t => { - const nyc = new NYC(configUtil.buildYargs().parse('--produce-source-map')) + const nyc = new NYC(await parseArgv(undefined, ['--produce-source-map'])) await nyc.reset() nyc.wrap() @@ -25,7 +24,7 @@ t.test('handles stack traces', async t => { }) t.test('does not handle stack traces when disabled', async t => { - const nyc = new NYC(configUtil.buildYargs().parse()) + const nyc = new NYC(await parseArgv()) await nyc.reset() nyc.wrap() diff --git a/test/wrap.js b/test/wrap.js index c78ea4fcb..2d28c2a2b 100644 --- a/test/wrap.js +++ b/test/wrap.js @@ -6,9 +6,8 @@ const path = require('path') const t = require('tap') const NYC = require('../self-coverage') -const configUtil = require('../self-coverage/lib/config-util') -const resetState = require('./helpers/reset-state') +const { parseArgv, resetState } = require('./helpers') // we test exit handlers in nyc-integration.js. NYC.prototype._wrapExit = () => {} @@ -19,7 +18,7 @@ const configMultExt = path.resolve(fixtures, 'conf-multiple-extensions') t.beforeEach(resetState) t.test('wraps modules with coverage counters when they are required', async t => { - const nyc = new NYC(configUtil.buildYargs().parse()) + const nyc = new NYC(await parseArgv()) await nyc.reset() nyc.wrap() @@ -36,7 +35,7 @@ t.test('wraps modules with coverage counters when the custom require hook compil module._compile(fs.readFileSync(filename, 'utf8'), filename) } - const nyc = new NYC(configUtil.buildYargs().parse()) + const nyc = new NYC(await parseArgv()) await nyc.reset() nyc.wrap() @@ -49,7 +48,7 @@ t.test('wraps modules with coverage counters when the custom require hook compil }) t.test('assigns a function to custom extensions', async t => { - const nyc = new NYC(configUtil.buildYargs(configMultExt).parse()) + const nyc = new NYC(await parseArgv(configMultExt)) await nyc.reset() nyc.wrap() @@ -62,7 +61,7 @@ t.test('assigns a function to custom extensions', async t => { t.test('calls the `_handleJs` function for custom file extensions', async t => { const required = {} - const nyc = new NYC(configUtil.buildYargs(configMultExt).parse()) + const nyc = new NYC(await parseArgv(configMultExt)) nyc._handleJs = (code, options) => { if (options.filename.includes('check-instrumented.es6')) { @@ -86,7 +85,7 @@ t.test('calls the `_handleJs` function for custom file extensions', async t => { }) t.test('does not output coverage for files that have not been included, by default', async t => { - const nyc = new NYC(configUtil.buildYargs(process.cwd()).parse()) + const nyc = new NYC(await parseArgv(process.cwd())) nyc.wrap() await nyc.reset() @@ -95,7 +94,7 @@ t.test('does not output coverage for files that have not been included, by defau }) t.test('tracks coverage appropriately once the file is required', async t => { - const nyc = new NYC(configUtil.buildYargs(fixtures).parse()) + const nyc = new NYC(await parseArgv(fixtures)) await nyc.reset() nyc.wrap()