Skip to content

Commit

Permalink
feat: nyc is being refactored to become the official Istanbul 1.0 bin (
Browse files Browse the repository at this point in the history
…#286)

BREAKING CHANGE:

significant chunks of nyc's API have been reworked, to use the Istanbul 1.0 API: source-map support, instrumentation, the check-coverage command, etc.
  • Loading branch information
bcoe authored Jul 4, 2016
1 parent 11a53c2 commit 61a05ea
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 640 deletions.
41 changes: 15 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# nyc

[![Build Status](https://travis-ci.org/bcoe/nyc.svg?branch=master)](https://travis-ci.org/bcoe/nyc)
[![Coverage Status](https://coveralls.io/repos/bcoe/nyc/badge.svg?branch=)](https://coveralls.io/r/bcoe/nyc?branch=)
[![Build Status](https://travis-ci.org/istanbuljs/nyc.svg?branch=master)](https://travis-ci.org/istanbuljs/nyc)
[![Coverage Status](https://coveralls.io/repos/istanbuljs/nyc/badge.svg?branch=)](https://coveralls.io/r/istanbuljs/nyc?branch=master)
[![NPM version](https://img.shields.io/npm/v/nyc.svg)](https://www.npmjs.com/package/nyc)
[![Windows Tests](https://img.shields.io/appveyor/ci/bcoe/nyc/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/bcoe/nyc)
[![Windows Tests](https://img.shields.io/appveyor/ci/istanbuljs/nyc/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/istanbuljs/nyc)
[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)

a code coverage tool built on [istanbul](https://www.npmjs.com/package/istanbul)
that works for applications that spawn subprocesses.
Istanbul's high-tech command line interface, with support for:

* applications that spawn subprocesses.
* ES2015 transforms, via [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul), or source-maps.

## Instrumenting Your Code

Expand Down Expand Up @@ -60,26 +62,27 @@ of the pre-transpiled code. You'll have to configure your custom require hook
to inline the source map in the transpiled code. For Babel that means setting
the `sourceMaps` option to `inline`.

## Use babel-plugin__coverage__ for Better ES6/ES7 Support
## Use with babel-plugin-istanbul for Better ES6/ES7 Support

[`babel-plugin-__coverage__`](https://github.com/dtinth/babel-plugin-__coverage__) can be used to enable better first-class ES6 support.
[`babel-plugin-istanbul`](https://github.com/istanbuljs/babel-plugin-istanbul) can be used to enable better first-class ES6 support.

1. enable the `__coverage__` plugin:
1. enable the `babel-plugin-istanbul` plugin:

```json
{
"babel": {
"presets": ["es2015"],
"env": {
"test": {
"plugins": ["__coverage__"]
"plugins": ["istanbul"]
}
}
}
}
```

Note: With this configuration, the `__coverage__` will only be active when `NODE_ENV` or `BABEL_ENV` is `test`.
Note: With this configuration, the Istanbul instrumentation will only be active when `NODE_ENV` or `BABEL_ENV` is `test`.

We recommend using the [`cross-env`](https://npmjs.com/package/cross-env) package to set these environment variables
in your `package.json` scripts in a way that works cross-platform.

Expand Down Expand Up @@ -125,15 +128,13 @@ nyc --extension .jsx --extension .es6 npm test

## Checking Coverage

nyc exposes istanbul's check-coverage tool. After running your tests with nyc,
simply run:
nyc can fail tests if coverage falls below a threshold.
After running your tests with nyc, simply run:

```shell
nyc check-coverage --lines 95 --functions 95 --branches 95
```

This feature makes it easy to fail your tests if coverage drops below a given threshold.

nyc also accepts a `--check-coverage` shorthand, which can be used to
both run tests and check that coverage falls within the threshold provided:

Expand Down Expand Up @@ -239,17 +240,6 @@ can also be specified in the `nyc` stanza of your package.json:
}
```

## Configuring Istanbul

Behind the scenes nyc uses [istanbul](https://www.npmjs.com/package/istanbul). You
can place a `.istanbul.yml` file in your project's root directory to pass config
setings to istanbul's code instrumenter:

```yml
instrumentation:
preserve-comments: true
```
## Integrating With Coveralls

[coveralls.io](https://coveralls.io) is a great tool for adding
Expand Down Expand Up @@ -285,7 +275,6 @@ That's all there is to it!
> Note: by default coveralls.io adds comments to pull-requests on GitHub, this can feel intrusive. To disable this, click on your repo on coveralls.io and uncheck `LEAVE COMMENTS?`.

## Integrating with codecov
`nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov`
Expand Down
47 changes: 18 additions & 29 deletions bin/nyc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ try {
NYC = require('../index.js')
}

var path = require('path')
var sw = require('spawn-wrap')
var wrapper = require.resolve('./wrap.js')

Expand Down Expand Up @@ -64,7 +63,7 @@ var yargs = require('yargs/yargs')(process.argv.slice(2))
})
.option('cache', {
alias: 'c',
default: false,
default: true,
type: 'boolean',
describe: 'cache instrumentation results for improved performance'
})
Expand Down Expand Up @@ -108,19 +107,14 @@ var yargs = require('yargs/yargs')(process.argv.slice(2))
type: 'boolean',
description: 'should nyc handle instrumentation?'
})
.option('instrumenter', {
default: './lib/instrumenters/istanbul',
type: 'string',
description: 'what library should be used to instrument coverage?'
})
.help('h')
.alias('h', 'help')
.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 --require --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')
.epilog('visit https://git.io/voHar for list of available reporters')

var argv = yargs.argv

Expand All @@ -141,12 +135,14 @@ if (argv._[0] === 'report') {
// if instrument is set to false,
// enable a noop instrumenter.
if (!argv.instrument) argv.instrumenter = './lib/instrumenters/noop'
else argv.instrumenter = './lib/instrumenters/istanbul'

var nyc = (new NYC({
require: argv.require,
include: argv.include,
exclude: argv.exclude,
sourceMap: !!argv.sourceMap
sourceMap: !!argv.sourceMap,
instrumenter: argv.instrumenter
}))
nyc.reset()

Expand Down Expand Up @@ -180,12 +176,10 @@ if (argv._[0] === 'report') {
var mainChildExitCode = process.exitCode

if (argv.checkCoverage) {
checkCoverage(argv, function (done) {
process.exitCode = process.exitCode || mainChildExitCode

if (!argv.silent) report(argv)
return done()
})
checkCoverage(argv)
process.exitCode = process.exitCode || mainChildExitCode
if (!argv.silent) report(argv)
return done()
} else {
if (!argv.silent) report(argv)
return done()
Expand All @@ -206,17 +200,12 @@ function report (argv) {
}

function checkCoverage (argv, cb) {
foreground(
process.execPath,
[
require.resolve('istanbul/lib/cli'),
'check-coverage',
'--lines=' + argv.lines,
'--functions=' + argv.functions,
'--branches=' + argv.branches,
'--statements=' + argv.statements,
path.resolve(process.cwd(), './.nyc_output/*.json')
],
cb
)
process.env.NYC_CWD = process.cwd()

;(new NYC()).checkCoverage({
lines: argv.lines,
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
})
}
3 changes: 1 addition & 2 deletions build-self-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ var fs = require('fs')
var path = require('path')

;[
'index.js',
'lib/source-map-cache.js'
'index.js'
].forEach(function (name) {
var indexPath = path.join(__dirname, name)
var source = fs.readFileSync(indexPath, 'utf8')
Expand Down
Loading

0 comments on commit 61a05ea

Please sign in to comment.