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

all of nyc's configuration options can now also be set in package.json #167

Merged
merged 2 commits into from
Feb 20, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ to inline the source map in the transpiled code. For Babel that means setting
the `sourceMaps` option to `inline`.

## Support For Custom File Extensions (.jsx, .es6)

Supporting file extensions can be configured through either the configuration arguments or with the `nyc` config section in `package.json`.

```shell
Expand Down Expand Up @@ -148,11 +149,11 @@ As an alternative to providing a list of files to `exclude`, you can provide
an `include` key to specify specific files that should be covered:

```json
{"config": {
{
"nyc": {
"include": ["**/build/umd/moment.js"]
}
}}
}
```

> Note: include defaults to `['**']`
Expand All @@ -175,6 +176,21 @@ You can run `nyc` with the optional `--cache` flag, to prevent it from
instrumenting the same files multiple times. This can signficantly
improve runtime performance.

## Configuring nyc

Any configuration options that can be set via the command line
can also be specified in the `nyc` stanza of your package.json:

```json
{
"nyc": {
"lines": 99,
"check-coverage": false,
"report-dir": "./alternative"
},
}
```

## Configuring Istanbul

Behind the scenes nyc uses [istanbul](https://www.npmjs.com/package/istanbul). You
Expand Down
59 changes: 17 additions & 42 deletions bin/nyc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,25 @@ var wrapper = require.resolve('./wrap.js')
var yargs = require('yargs')
.usage('$0 [command] [options]\n\nrun your tests with the nyc bin to instrument them with coverage')
.command('report', 'run coverage report for .nyc_output', function (yargs) {
yargs
return yargs
.usage('$0 report [options]')
.option('r', {
alias: 'reporter',
describe: 'coverage reporter(s) to use',
default: 'text'
})
.option('report-dir', {
describe: 'default directory to output coverage reports in',
default: 'coverage'
})
.help('h')
.alias('h', 'help')
.example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')
})
.command('check-coverage', 'check whether coverage is within thresholds provided', function (yargs) {
yargs
return yargs
.usage('$0 check-coverage [options]')
.option('b', {
alias: 'branches',
default: 0,
description: 'what % of branches must be covered?'
})
.option('f', {
alias: 'functions',
default: 0,
description: 'what % of functions must be covered?'
})
.option('l', {
alias: 'lines',
default: 90,
description: 'what % of lines must be covered?'
})
.option('s', {
alias: 'statements',
default: 0,
description: 'what % of statements must be covered?'
})
.help('h')
.alias('h', 'help')
.example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided")
})
.option('r', {
alias: 'reporter',
describe: 'coverage reporter(s) to use',
default: 'text'
default: 'text',
global: true
})
.option('report-dir', {
describe: 'default directory to output coverage reports in',
default: 'coverage'
default: 'coverage',
global: true
})
.option('s', {
alias: 'silent',
Expand Down Expand Up @@ -100,27 +69,33 @@ var yargs = require('yargs')
})
.option('branches', {
default: 0,
description: 'what % of branches must be covered?'
description: 'what % of branches must be covered?',
global: true
})
.option('functions', {
default: 0,
description: 'what % of functions must be covered?'
description: 'what % of functions must be covered?',
global: true
})
.option('lines', {
default: 90,
description: 'what % of lines must be covered?'
description: 'what % of lines must be covered?',
global: true
})
.option('statements', {
default: 0,
description: 'what % of statements must be covered?'
description: 'what % of statements must be covered?',
global: true
})
.help('h')
.alias('h', 'help')
.version(require('../package.json').version)
.version()
.pkgConf('nyc', process.cwd())
.example('$0 npm test', 'instrument your tests with coverage')
.example('$0 --require babel-core/polyfill --require babel-core/register npm test', 'instrument your tests with coverage and babel')
.example('$0 report --reporter=text-lcov', 'output lcov report after running your tests')
.epilog('visit http://git.io/vTJJB for list of available reporters')

var argv = yargs.argv

if (argv._[0] === 'report') {
Expand Down
36 changes: 16 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,25 @@ var SourceMapCache = require('./lib/source-map-cache')
var convertSourceMap = require('convert-source-map')
var md5hex = require('md5-hex')
var findCacheDir = require('find-cache-dir')
var pkgUp = require('pkg-up')
var readPkg = require('read-pkg')
var js = require('default-require-extensions/js')
var pkgUp = require('pkg-up')
var yargs = require('yargs')

/* istanbul ignore next */
if (/index\.covered\.js$/.test(__filename)) {
require('./lib/self-coverage-helper')
}

function NYC (opts) {
opts = opts || {}

this._istanbul = opts.istanbul
this.subprocessBin = opts.subprocessBin || path.resolve(__dirname, './bin/nyc.js')
this._tempDirectory = opts.tempDirectory || './.nyc_output'
this._reportDir = opts.reportDir
var config = this._loadConfig(opts || {})

var config = this._loadConfig(opts)
this._istanbul = config.istanbul
this.subprocessBin = config.subprocessBin || path.resolve(__dirname, './bin/nyc.js')
this._tempDirectory = config.tempDirectory || './.nyc_output'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is path.resolve used on subprocessBin but not subprocessBin?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jameswomack I think the reasoning was that:

  • if you explicitly specify the path to the bin that should be run, we assume you know the path that you want.
  • otherwise we default to the bin that we know will be relative to index.js.

this._reportDir = config.reportDir
this.cwd = config.cwd

this.reporter = arrify(opts.reporter || 'text')
this.reporter = arrify(config.reporter || 'text')

// load exclude stanza from config.
this.include = false
Expand All @@ -49,12 +47,12 @@ function NYC (opts) {

this.cacheDirectory = findCacheDir({name: 'nyc', cwd: this.cwd})

this.enableCache = Boolean(this.cacheDirectory && (opts.enableCache === true || process.env.NYC_CACHE === 'enable'))
this.enableCache = Boolean(this.cacheDirectory && (config.enableCache === true || process.env.NYC_CACHE === 'enable'))

// require extensions can be provided as config in package.json.
this.require = arrify(config.require || opts.require)
this.require = arrify(config.require)

this.extensions = arrify(config.extension || opts.extension).concat('.js').map(function (ext) {
this.extensions = arrify(config.extension).concat('.js').map(function (ext) {
return ext.toLowerCase()
})

Expand All @@ -73,18 +71,16 @@ NYC.prototype._loadConfig = function (opts) {
var cwd = opts.cwd || process.env.NYC_CWD || process.cwd()
var pkgPath = pkgUp.sync(cwd)

// you can specify config in the nyc stanza of package.json.
var config
if (pkgPath) {
cwd = path.dirname(pkgPath)
var pkg = readPkg.sync(pkgPath, {normalize: false})
config = pkg.nyc || (pkg.config && pkg.config.nyc)
}
config = config || {}

config.cwd = cwd
opts.cwd = cwd

return config
return yargs([])
.pkgConf('nyc', cwd)
.default(opts)
.argv
}

NYC.prototype._createTransform = function (ext) {
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
"test/source-map-cache.js",
"index.covered.js",
"test/fixtures/_generateCoverage.js"
],
"extension": [".es6"]
]
},
"standard": {
"ignore": [
Expand Down Expand Up @@ -79,18 +78,18 @@
"micromatch": "~2.1.6",
"mkdirp": "^0.5.0",
"pkg-up": "^1.0.0",
"read-pkg": "^1.1.0",
"resolve-from": "^2.0.0",
"rimraf": "^2.5.0",
"signal-exit": "^2.1.1",
"source-map": "^0.5.3",
"spawn-wrap": "^1.1.1",
"strip-bom": "^2.0.0",
"yargs": "^3.15.0"
"yargs": "^4.0.0"
},
"devDependencies": {
"any-path": "^1.3.0",
"chai": "^3.0.0",
"clear-require": "^1.0.1",
"coveralls": "^2.11.4",
"exists-sync": "0.0.3",
"forking-tap": "^0.1.1",
Expand Down
14 changes: 6 additions & 8 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
"nyc": "./bin/nyc.js",
"nyc-report": "./bin/nyc-report.js"
},
"config": {
"nyc": {
"exclude": [
"**/blarg",
"**/blerg"
],
"extension": [".es6", ".foo.BAR"]
}
"nyc": {
"exclude": [
"**/blarg",
"**/blerg"
],
"extension": [".es6", ".foo.BAR"]
}
}
22 changes: 4 additions & 18 deletions test/src/nyc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function NYC (opts) {
}

var path = require('path')
var clearRequire = require('clear-require')
var existsSync = require('exists-sync')
var rimraf = require('rimraf')
var sinon = require('sinon')
Expand Down Expand Up @@ -68,37 +69,21 @@ describe('nyc', function () {
})

describe('config', function () {
it("loads 'exclude' patterns from package.json#config.nyc", function () {
it("loads 'exclude' patterns from package.json#nyc", function () {
var nyc = new NYC({
cwd: path.resolve(__dirname, '../fixtures')
})

nyc.exclude.length.should.eql(5)
})

it("loads 'exclude' patterns from package.json#nyc", function () {
var nyc = new NYC({
cwd: path.resolve(__dirname, '../..')
})

nyc.exclude.length.should.eql(19)
})

it("loads 'extension' patterns from package.json#config.nyc", function () {
it("loads 'extension' patterns from package.json#nyc", function () {
var nyc = new NYC({
cwd: path.resolve(__dirname, '../fixtures')
})

nyc.extensions.length.should.eql(3)
})

it("loads 'extension' from package.json#nyc", function () {
var nyc = new NYC({
cwd: path.resolve(__dirname, '../..')
})

nyc.extensions.length.should.eql(2)
})
})

describe('_prepGlobPatterns', function () {
Expand Down Expand Up @@ -532,6 +517,7 @@ describe('nyc', function () {
'test/nyc-test.js'
]

clearRequire('yargs')
var yargv = require('yargs').argv

var munged = (new NYC()).mungeArgs(yargv)
Expand Down