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 nyc.config.js #1019

Merged
merged 1 commit into from Mar 11, 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
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions lib/config-util.js
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cli/nyc-config-js/ignore.js
@@ -0,0 +1 @@
var i = 2
11 changes: 11 additions & 0 deletions test/fixtures/cli/nyc-config-js/index.js
@@ -0,0 +1,11 @@
require('./ignore')

var a = 0

a++

if (a === 0) {
a++;
a--;
a++;
}
3 changes: 3 additions & 0 deletions test/fixtures/cli/nyc-config-js/nyc.config.js
@@ -0,0 +1,3 @@
module.exports = {
exclude: ['nyc.config.js', 'nycrc-config.js', 'ignore.js']
};
9 changes: 9 additions & 0 deletions 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: []
}
5 changes: 5 additions & 0 deletions test/fixtures/cli/nyc-config-js/package.json
@@ -0,0 +1,5 @@
{
"nyc": {
"reporter": ["text-lcov"]
}
}
74 changes: 74 additions & 0 deletions test/nyc-integration.js
Expand Up @@ -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')

Expand Down