Skip to content

Commit 3b203c7

Browse files
coreyfarrellJaKXz
authored andcommitted
feat: Add support for nyc.config.js (#1019)
1 parent b25492a commit 3b203c7

File tree

8 files changed

+131
-4
lines changed

8 files changed

+131
-4
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,21 @@ Any configuration options that can be set via the command line can also be speci
320320
}
321321
```
322322

323+
Configuration can also be provided by `nyc.config.js` if programmed logic is required:
324+
```js
325+
'use strict';
326+
const {defaultExclude} = require('test-exclude');
327+
const isWindows = require('is-windows');
328+
329+
let platformExclude = [
330+
isWindows() ? 'lib/posix.js' : 'lib/win32.js'
331+
];
332+
333+
module.exports = {
334+
exclude: platformExclude.concat(defaultExclude)
335+
};
336+
```
337+
323338
### Publish, and reuse, your nyc configuration
324339

325340
nyc allows you to inherit other configurations using the key `extends`. As an example,

lib/config-util.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@ function guessCWD (cwd) {
1919
}
2020

2121
Config.loadConfig = function (argv, cwd) {
22-
const rcPath = findUp.sync([argv.nycrcPath || '.nycrc', '.nycrc.json'], { cwd: guessCWD(cwd) })
22+
const rcOptions = [
23+
argv.nycrcPath || '.nycrc',
24+
'.nycrc.json',
25+
'nyc.config.js'
26+
]
27+
const rcPath = findUp.sync(rcOptions, { cwd: guessCWD(cwd) })
2328
let config = {}
2429

2530
if (rcPath) {
26-
config = JSON.parse(
27-
fs.readFileSync(rcPath, 'utf-8')
28-
)
31+
if (rcPath.toLowerCase().endsWith('.js')) {
32+
config = require(rcPath)
33+
} else {
34+
config = JSON.parse(
35+
fs.readFileSync(rcPath, 'utf-8')
36+
)
37+
}
2938
}
3039

3140
if (config.require) config.require = arrify(config.require)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var i = 2
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require('./ignore')
2+
3+
var a = 0
4+
5+
a++
6+
7+
if (a === 0) {
8+
a++;
9+
a--;
10+
a++;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
exclude: ['nyc.config.js', 'nycrc-config.js', 'ignore.js']
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
'check-coverage': true,
3+
'per-file': true,
4+
lines: 100,
5+
statements: 100,
6+
functions: 100,
7+
branches: 100,
8+
exclude: []
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"nyc": {
3+
"reporter": ["text-lcov"]
4+
}
5+
}

test/nyc-integration.js

+74
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,80 @@ describe('the nyc cli', function () {
367367
})
368368
})
369369

370+
describe('nyc.config.js', function () {
371+
var cwd = path.resolve(fixturesCLI, './nyc-config-js')
372+
373+
it('loads configuration from package.json and nyc.config.js', function (done) {
374+
var args = [bin, process.execPath, './index.js']
375+
376+
var proc = spawn(process.execPath, args, {
377+
cwd: cwd,
378+
env: env
379+
})
380+
381+
var stdout = ''
382+
proc.stdout.on('data', function (chunk) {
383+
stdout += chunk
384+
})
385+
386+
proc.on('close', function (code) {
387+
code.should.equal(0)
388+
stdout.should.match(/SF:.*index\.js/)
389+
stdout.should.not.match(/SF:.*ignore\.js/)
390+
stdout.should.not.match(/SF:.*nyc\.config\.js/)
391+
stdout.should.not.match(/SF:.*nycrc-config\.js/)
392+
done()
393+
})
394+
})
395+
396+
it('loads configuration from different module rather than nyc.config.js', function (done) {
397+
var args = [bin, '--all', '--nycrc-path', './nycrc-config.js', process.execPath, './index.js']
398+
399+
var proc = spawn(process.execPath, args, {
400+
cwd: cwd,
401+
env: env
402+
})
403+
404+
var stdout = ''
405+
proc.stdout.on('data', function (chunk) {
406+
stdout += chunk
407+
})
408+
409+
proc.on('close', function (code) {
410+
// should be 1 due to coverage check
411+
code.should.equal(1)
412+
stdout.should.match(/SF:.*index\.js/)
413+
stdout.should.match(/SF:.*ignore\.js/)
414+
stdout.should.match(/SF:.*nyc\.config\.js/)
415+
stdout.should.match(/SF:.*nycrc-config\.js/)
416+
done()
417+
})
418+
})
419+
420+
it('allows nyc.config.js configuration to be overridden with command line args', function (done) {
421+
var args = [bin, '--all', '--exclude=foo.js', process.execPath, './index.js']
422+
423+
var proc = spawn(process.execPath, args, {
424+
cwd: cwd,
425+
env: env
426+
})
427+
428+
var stdout = ''
429+
proc.stdout.on('data', function (chunk) {
430+
stdout += chunk
431+
})
432+
433+
proc.on('close', function (code) {
434+
code.should.equal(0)
435+
stdout.should.match(/SF:.*index\.js/)
436+
stdout.should.match(/SF:.*ignore\.js/)
437+
stdout.should.match(/SF:.*nyc\.config\.js/)
438+
stdout.should.match(/SF:.*nycrc-config\.js/)
439+
done()
440+
})
441+
})
442+
})
443+
370444
describe('.nycrc', function () {
371445
var cwd = path.resolve(fixturesCLI, './nycrc')
372446

0 commit comments

Comments
 (0)