From 3b203c7f57f8074ec10172a772b766f92570a1aa Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Mon, 11 Mar 2019 00:39:31 -0400 Subject: [PATCH] feat: Add support for nyc.config.js (#1019) --- README.md | 15 ++++ lib/config-util.js | 17 ++++- test/fixtures/cli/nyc-config-js/ignore.js | 1 + test/fixtures/cli/nyc-config-js/index.js | 11 +++ test/fixtures/cli/nyc-config-js/nyc.config.js | 3 + .../cli/nyc-config-js/nycrc-config.js | 9 +++ test/fixtures/cli/nyc-config-js/package.json | 5 ++ test/nyc-integration.js | 74 +++++++++++++++++++ 8 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/cli/nyc-config-js/ignore.js create mode 100644 test/fixtures/cli/nyc-config-js/index.js create mode 100644 test/fixtures/cli/nyc-config-js/nyc.config.js create mode 100644 test/fixtures/cli/nyc-config-js/nycrc-config.js create mode 100644 test/fixtures/cli/nyc-config-js/package.json diff --git a/README.md b/README.md index 9ed35e72b..82b4289c3 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,21 @@ Any configuration options that can be set via the command line can also be speci } ``` +Configuration can also be provided by `nyc.config.js` if programmed logic is required: +```js +'use strict'; +const {defaultExclude} = require('test-exclude'); +const isWindows = require('is-windows'); + +let platformExclude = [ + isWindows() ? 'lib/posix.js' : 'lib/win32.js' +]; + +module.exports = { + exclude: platformExclude.concat(defaultExclude) +}; +``` + ### Publish, and reuse, your nyc configuration nyc allows you to inherit other configurations using the key `extends`. As an example, diff --git a/lib/config-util.js b/lib/config-util.js index ac3e417da..37f726781 100644 --- a/lib/config-util.js +++ b/lib/config-util.js @@ -19,13 +19,22 @@ function guessCWD (cwd) { } Config.loadConfig = function (argv, cwd) { - const rcPath = findUp.sync([argv.nycrcPath || '.nycrc', '.nycrc.json'], { cwd: guessCWD(cwd) }) + const rcOptions = [ + argv.nycrcPath || '.nycrc', + '.nycrc.json', + 'nyc.config.js' + ] + const rcPath = findUp.sync(rcOptions, { cwd: guessCWD(cwd) }) let config = {} if (rcPath) { - config = JSON.parse( - fs.readFileSync(rcPath, 'utf-8') - ) + if (rcPath.toLowerCase().endsWith('.js')) { + config = require(rcPath) + } else { + config = JSON.parse( + fs.readFileSync(rcPath, 'utf-8') + ) + } } if (config.require) config.require = arrify(config.require) diff --git a/test/fixtures/cli/nyc-config-js/ignore.js b/test/fixtures/cli/nyc-config-js/ignore.js new file mode 100644 index 000000000..2ace002b5 --- /dev/null +++ b/test/fixtures/cli/nyc-config-js/ignore.js @@ -0,0 +1 @@ +var i = 2 diff --git a/test/fixtures/cli/nyc-config-js/index.js b/test/fixtures/cli/nyc-config-js/index.js new file mode 100644 index 000000000..8f4dfee22 --- /dev/null +++ b/test/fixtures/cli/nyc-config-js/index.js @@ -0,0 +1,11 @@ +require('./ignore') + +var a = 0 + +a++ + +if (a === 0) { + a++; + a--; + a++; +} diff --git a/test/fixtures/cli/nyc-config-js/nyc.config.js b/test/fixtures/cli/nyc-config-js/nyc.config.js new file mode 100644 index 000000000..cc19aea0c --- /dev/null +++ b/test/fixtures/cli/nyc-config-js/nyc.config.js @@ -0,0 +1,3 @@ +module.exports = { + exclude: ['nyc.config.js', 'nycrc-config.js', 'ignore.js'] +}; diff --git a/test/fixtures/cli/nyc-config-js/nycrc-config.js b/test/fixtures/cli/nyc-config-js/nycrc-config.js new file mode 100644 index 000000000..3c92a4454 --- /dev/null +++ b/test/fixtures/cli/nyc-config-js/nycrc-config.js @@ -0,0 +1,9 @@ +module.exports = { + 'check-coverage': true, + 'per-file': true, + lines: 100, + statements: 100, + functions: 100, + branches: 100, + exclude: [] +} diff --git a/test/fixtures/cli/nyc-config-js/package.json b/test/fixtures/cli/nyc-config-js/package.json new file mode 100644 index 000000000..d73c43154 --- /dev/null +++ b/test/fixtures/cli/nyc-config-js/package.json @@ -0,0 +1,5 @@ +{ + "nyc": { + "reporter": ["text-lcov"] + } +} diff --git a/test/nyc-integration.js b/test/nyc-integration.js index 3139bc346..0eba6aaa5 100644 --- a/test/nyc-integration.js +++ b/test/nyc-integration.js @@ -367,6 +367,80 @@ describe('the nyc cli', function () { }) }) + describe('nyc.config.js', function () { + var cwd = path.resolve(fixturesCLI, './nyc-config-js') + + it('loads configuration from package.json and nyc.config.js', function (done) { + var args = [bin, process.execPath, './index.js'] + + var proc = spawn(process.execPath, args, { + cwd: cwd, + env: env + }) + + var stdout = '' + proc.stdout.on('data', function (chunk) { + stdout += chunk + }) + + proc.on('close', function (code) { + code.should.equal(0) + stdout.should.match(/SF:.*index\.js/) + stdout.should.not.match(/SF:.*ignore\.js/) + stdout.should.not.match(/SF:.*nyc\.config\.js/) + stdout.should.not.match(/SF:.*nycrc-config\.js/) + done() + }) + }) + + it('loads configuration from different module rather than nyc.config.js', function (done) { + var args = [bin, '--all', '--nycrc-path', './nycrc-config.js', process.execPath, './index.js'] + + var proc = spawn(process.execPath, args, { + cwd: cwd, + env: env + }) + + var stdout = '' + proc.stdout.on('data', function (chunk) { + stdout += chunk + }) + + proc.on('close', function (code) { + // should be 1 due to coverage check + code.should.equal(1) + stdout.should.match(/SF:.*index\.js/) + stdout.should.match(/SF:.*ignore\.js/) + stdout.should.match(/SF:.*nyc\.config\.js/) + stdout.should.match(/SF:.*nycrc-config\.js/) + done() + }) + }) + + it('allows nyc.config.js configuration to be overridden with command line args', function (done) { + var args = [bin, '--all', '--exclude=foo.js', process.execPath, './index.js'] + + var proc = spawn(process.execPath, args, { + cwd: cwd, + env: env + }) + + var stdout = '' + proc.stdout.on('data', function (chunk) { + stdout += chunk + }) + + proc.on('close', function (code) { + code.should.equal(0) + stdout.should.match(/SF:.*index\.js/) + stdout.should.match(/SF:.*ignore\.js/) + stdout.should.match(/SF:.*nyc\.config\.js/) + stdout.should.match(/SF:.*nycrc-config\.js/) + done() + }) + }) + }) + describe('.nycrc', function () { var cwd = path.resolve(fixturesCLI, './nycrc')