Skip to content

Commit 1c2349b

Browse files
authored
feat: add support for .nycrc (#391)
1 parent bc0a99a commit 1c2349b

File tree

8 files changed

+97
-7
lines changed

8 files changed

+97
-7
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ improve runtime performance.
226226

227227
## Configuring `nyc`
228228

229-
Any configuration options that can be set via the command line can also be specified in the `nyc` stanza of your package.json (these will not affect `nyc` subcommands):
229+
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` file:
230+
231+
**package.json:**
230232

231233
```json
232234
{
@@ -260,6 +262,20 @@ Any configuration options that can be set via the command line can also be speci
260262
}
261263
```
262264

265+
**.nycrc:**
266+
267+
```json
268+
{
269+
"reporter": [
270+
"lcov",
271+
"text-summary"
272+
],
273+
"require": [
274+
"./test/helpers/some-helper.js"
275+
]
276+
}
277+
```
278+
263279
## Instrumenting source files
264280

265281
nyc's `instrument` command can be used to instrument

lib/config-util.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var arrify = require('arrify')
2+
var fs = require('fs')
23
var path = require('path')
3-
var pkgUp = require('pkg-up')
4+
var findUp = require('find-up')
45
var testExclude = require('test-exclude')
56
var Yargs = require('yargs/yargs')
67

@@ -12,7 +13,15 @@ var Config = {}
1213
// * .nycrc (coming soon)
1314
Config.loadConfig = function (argv, cwd) {
1415
cwd = cwd || process.env.NYC_CWD || process.cwd()
15-
var pkgPath = pkgUp.sync(cwd)
16+
var pkgPath = findUp.sync('package.json', {cwd: cwd})
17+
var rcPath = findUp.sync('.nycrc', {cwd: cwd})
18+
var rcConfig = null
19+
20+
if (rcPath) {
21+
rcConfig = JSON.parse(
22+
fs.readFileSync(rcPath, 'utf-8')
23+
)
24+
}
1625

1726
if (pkgPath) {
1827
cwd = path.dirname(pkgPath)
@@ -22,7 +31,8 @@ Config.loadConfig = function (argv, cwd) {
2231
.default({
2332
cwd: cwd
2433
})
25-
.parse(argv || [])
34+
if (rcConfig) config.config(rcConfig)
35+
config = config.parse(argv || [])
2636

2737
// post-hoc, we convert several of the
2838
// configuration settings to arrays, providing

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
"md5-hex": "^1.2.0",
9191
"micromatch": "^2.3.11",
9292
"mkdirp": "^0.5.0",
93-
"pkg-up": "^1.0.0",
9493
"resolve-from": "^2.0.0",
9594
"rimraf": "^2.5.4",
9695
"signal-exit": "^3.0.1",
@@ -143,7 +142,6 @@
143142
"md5-hex",
144143
"micromatch",
145144
"mkdirp",
146-
"pkg-up",
147145
"resolve-from",
148146
"rimraf",
149147
"signal-exit",
@@ -152,4 +150,4 @@
152150
"yargs",
153151
"yargs-parser"
154152
]
155-
}
153+
}

test/fixtures/cli/nycrc/.nycrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exclude": ["ignore.js"]
3+
}

test/fixtures/cli/nycrc/ignore.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var i = 2

test/fixtures/cli/nycrc/index.js

+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+
}

test/fixtures/cli/nycrc/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"nyc": {
3+
"reporter": ["text-lcov"]
4+
}
5+
}

test/src/nyc-bin.js

+46
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,52 @@ describe('the nyc cli', function () {
284284
done()
285285
})
286286
})
287+
288+
describe('.nycrc', function () {
289+
var cwd = path.resolve(fixturesCLI, './nycrc')
290+
291+
it('loads configuration from package.json and .nycrc', function (done) {
292+
var args = [bin, process.execPath, './index.js']
293+
294+
var proc = spawn(process.execPath, args, {
295+
cwd: cwd,
296+
env: env
297+
})
298+
299+
var stdout = ''
300+
proc.stdout.on('data', function (chunk) {
301+
stdout += chunk
302+
})
303+
304+
proc.on('close', function (code) {
305+
code.should.equal(0)
306+
stdout.should.match(/SF:.*index\.js/)
307+
stdout.should.not.match(/SF:.*ignore\.js/)
308+
done()
309+
})
310+
})
311+
312+
it('allows .nycrc configuration to be overridden with command line args', function (done) {
313+
var args = [bin, '--exclude=foo.js', process.execPath, './index.js']
314+
315+
var proc = spawn(process.execPath, args, {
316+
cwd: cwd,
317+
env: env
318+
})
319+
320+
var stdout = ''
321+
proc.stdout.on('data', function (chunk) {
322+
stdout += chunk
323+
})
324+
325+
proc.on('close', function (code) {
326+
code.should.equal(0)
327+
stdout.should.match(/SF:.*index\.js/)
328+
stdout.should.match(/SF:.*ignore\.js/)
329+
done()
330+
})
331+
})
332+
})
287333
})
288334

289335
it('setting instrument to "false" sets noop instrumenter', function (done) {

0 commit comments

Comments
 (0)