Skip to content

Commit ca37ffa

Browse files
furudeanJaKXz
authored andcommitted
feat: add support for yaml configuration file (#1054)
1 parent c4fcf5e commit ca37ffa

File tree

7 files changed

+69
-11
lines changed

7 files changed

+69
-11
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,15 @@ modules should be required in the subprocess collecting coverage:
341341

342342
## Configuring `nyc`
343343

344-
Any configuration options that can be set via the command line can also be specified in the `nyc` stanza of your package.json, or within a `.nycrc`, `.nycrc.json`, or `nyc.config.js` file:
344+
Any configuration options that can be set via the command line can also be specified in the `nyc` stanza of your package.json, or within a seperate configuration file - a variety of flavors are available:
345+
346+
| File name | File Association |
347+
|-----------------|------------------|
348+
| `.nycrc` | JSON |
349+
| `.nycrc.json` | JSON |
350+
| `.nycrc.yaml` | YAML |
351+
| `.nycrc.yml` | YAML |
352+
| `nyc.config.js` | CommonJS export |
345353

346354
**package.json:**
347355

lib/config-util.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@ Config.loadConfig = function (argv, cwd) {
2121
const rcOptions = [
2222
argv.nycrcPath || '.nycrc',
2323
'.nycrc.json',
24+
'.nycrc.yml',
25+
'.nycrc.yaml',
2426
'nyc.config.js'
2527
]
2628
const rcPath = findUp.sync(rcOptions, { cwd: guessCWD(cwd) })
2729
let config = {}
2830

2931
if (rcPath) {
30-
if (rcPath.toLowerCase().endsWith('.js')) {
32+
const rcExt = path.extname(rcPath.toLowerCase())
33+
if (rcExt === '.js') {
3134
config = require(rcPath)
35+
} else if (rcExt === '.yml' || rcExt === '.yaml') {
36+
config = require('js-yaml').load(
37+
fs.readFileSync(rcPath, 'utf8')
38+
)
3239
} else {
3340
config = JSON.parse(
3441
fs.readFileSync(rcPath, 'utf-8')

package-lock.json

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

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"istanbul-lib-report": "^2.0.7",
8181
"istanbul-lib-source-maps": "^3.0.5",
8282
"istanbul-reports": "^2.2.2",
83+
"js-yaml": "^3.13.1",
8384
"make-dir": "^2.1.0",
8485
"merge-source-map": "^1.1.0",
8586
"resolve-from": "^4.0.0",

test/fixtures/cli/nycrc/.nycrc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exclude:
2+
- ignore.js

test/fixtures/cli/nycrc/.nycrc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exclude:
2+
- ignore.js

test/nyc-integration.js

+42
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,48 @@ describe('the nyc cli', function () {
488488
})
489489
})
490490

491+
it('loads configuration from .nycrc.yml', function (done) {
492+
var args = [bin, '--nycrc-path', './.nycrc.yml', process.execPath, './index.js']
493+
494+
var proc = spawn(process.execPath, args, {
495+
cwd: cwd,
496+
env: env
497+
})
498+
499+
var stdout = ''
500+
proc.stdout.on('data', function (chunk) {
501+
stdout += chunk
502+
})
503+
504+
proc.on('close', function (code) {
505+
code.should.equal(0)
506+
stdout.should.match(/SF:.*index\.js/)
507+
stdout.should.not.match(/SF:.*ignore\.js/)
508+
done()
509+
})
510+
})
511+
512+
it('loads configuration from .nycrc.yaml', function (done) {
513+
var args = [bin, '--nycrc-path', './.nycrc.yaml', process.execPath, './index.js']
514+
515+
var proc = spawn(process.execPath, args, {
516+
cwd: cwd,
517+
env: env
518+
})
519+
520+
var stdout = ''
521+
proc.stdout.on('data', function (chunk) {
522+
stdout += chunk
523+
})
524+
525+
proc.on('close', function (code) {
526+
code.should.equal(0)
527+
stdout.should.match(/SF:.*index\.js/)
528+
stdout.should.not.match(/SF:.*ignore\.js/)
529+
done()
530+
})
531+
})
532+
491533
it('allows .nycrc configuration to be overridden with command line args', function (done) {
492534
var args = [bin, '--exclude=foo.js', process.execPath, './index.js']
493535

0 commit comments

Comments
 (0)