Skip to content

Commit 61a05ea

Browse files
authored
feat: nyc is being refactored to become the official Istanbul 1.0 bin (#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.
1 parent 11a53c2 commit 61a05ea

13 files changed

+195
-640
lines changed

README.md

+15-26
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# nyc
22

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

9-
a code coverage tool built on [istanbul](https://www.npmjs.com/package/istanbul)
10-
that works for applications that spawn subprocesses.
9+
Istanbul's high-tech command line interface, with support for:
10+
11+
* applications that spawn subprocesses.
12+
* ES2015 transforms, via [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul), or source-maps.
1113

1214
## Instrumenting Your Code
1315

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

63-
## Use babel-plugin__coverage__ for Better ES6/ES7 Support
65+
## Use with babel-plugin-istanbul for Better ES6/ES7 Support
6466

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

67-
1. enable the `__coverage__` plugin:
69+
1. enable the `babel-plugin-istanbul` plugin:
6870

6971
```json
7072
{
7173
"babel": {
7274
"presets": ["es2015"],
7375
"env": {
7476
"test": {
75-
"plugins": ["__coverage__"]
77+
"plugins": ["istanbul"]
7678
}
7779
}
7880
}
7981
}
8082
```
8183

82-
Note: With this configuration, the `__coverage__` will only be active when `NODE_ENV` or `BABEL_ENV` is `test`.
84+
Note: With this configuration, the Istanbul instrumentation will only be active when `NODE_ENV` or `BABEL_ENV` is `test`.
85+
8386
We recommend using the [`cross-env`](https://npmjs.com/package/cross-env) package to set these environment variables
8487
in your `package.json` scripts in a way that works cross-platform.
8588

@@ -125,15 +128,13 @@ nyc --extension .jsx --extension .es6 npm test
125128

126129
## Checking Coverage
127130

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

131134
```shell
132135
nyc check-coverage --lines 95 --functions 95 --branches 95
133136
```
134137

135-
This feature makes it easy to fail your tests if coverage drops below a given threshold.
136-
137138
nyc also accepts a `--check-coverage` shorthand, which can be used to
138139
both run tests and check that coverage falls within the threshold provided:
139140

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

242-
## Configuring Istanbul
243-
244-
Behind the scenes nyc uses [istanbul](https://www.npmjs.com/package/istanbul). You
245-
can place a `.istanbul.yml` file in your project's root directory to pass config
246-
setings to istanbul's code instrumenter:
247-
248-
```yml
249-
instrumentation:
250-
preserve-comments: true
251-
```
252-
253243
## Integrating With Coveralls
254244

255245
[coveralls.io](https://coveralls.io) is a great tool for adding
@@ -285,7 +275,6 @@ That's all there is to it!
285275
286276
> 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?`.
287277

288-
289278
## Integrating with codecov
290279

291280
`nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov`

bin/nyc.js

+18-29
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ try {
88
NYC = require('../index.js')
99
}
1010

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

@@ -64,7 +63,7 @@ var yargs = require('yargs/yargs')(process.argv.slice(2))
6463
})
6564
.option('cache', {
6665
alias: 'c',
67-
default: false,
66+
default: true,
6867
type: 'boolean',
6968
describe: 'cache instrumentation results for improved performance'
7069
})
@@ -108,19 +107,14 @@ var yargs = require('yargs/yargs')(process.argv.slice(2))
108107
type: 'boolean',
109108
description: 'should nyc handle instrumentation?'
110109
})
111-
.option('instrumenter', {
112-
default: './lib/instrumenters/istanbul',
113-
type: 'string',
114-
description: 'what library should be used to instrument coverage?'
115-
})
116110
.help('h')
117111
.alias('h', 'help')
118112
.version()
119113
.pkgConf('nyc', process.cwd())
120114
.example('$0 npm test', 'instrument your tests with coverage')
121-
.example('$0 --require babel-core/polyfill --require babel-core/register npm test', 'instrument your tests with coverage and babel')
115+
.example('$0 --require --require babel-core/register npm test', 'instrument your tests with coverage and babel')
122116
.example('$0 report --reporter=text-lcov', 'output lcov report after running your tests')
123-
.epilog('visit http://git.io/vTJJB for list of available reporters')
117+
.epilog('visit https://git.io/voHar for list of available reporters')
124118

125119
var argv = yargs.argv
126120

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

145140
var nyc = (new NYC({
146141
require: argv.require,
147142
include: argv.include,
148143
exclude: argv.exclude,
149-
sourceMap: !!argv.sourceMap
144+
sourceMap: !!argv.sourceMap,
145+
instrumenter: argv.instrumenter
150146
}))
151147
nyc.reset()
152148

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

182178
if (argv.checkCoverage) {
183-
checkCoverage(argv, function (done) {
184-
process.exitCode = process.exitCode || mainChildExitCode
185-
186-
if (!argv.silent) report(argv)
187-
return done()
188-
})
179+
checkCoverage(argv)
180+
process.exitCode = process.exitCode || mainChildExitCode
181+
if (!argv.silent) report(argv)
182+
return done()
189183
} else {
190184
if (!argv.silent) report(argv)
191185
return done()
@@ -206,17 +200,12 @@ function report (argv) {
206200
}
207201

208202
function checkCoverage (argv, cb) {
209-
foreground(
210-
process.execPath,
211-
[
212-
require.resolve('istanbul/lib/cli'),
213-
'check-coverage',
214-
'--lines=' + argv.lines,
215-
'--functions=' + argv.functions,
216-
'--branches=' + argv.branches,
217-
'--statements=' + argv.statements,
218-
path.resolve(process.cwd(), './.nyc_output/*.json')
219-
],
220-
cb
221-
)
203+
process.env.NYC_CWD = process.cwd()
204+
205+
;(new NYC()).checkCoverage({
206+
lines: argv.lines,
207+
functions: argv.functions,
208+
branches: argv.branches,
209+
statements: argv.statements
210+
})
222211
}

build-self-coverage.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ var fs = require('fs')
33
var path = require('path')
44

55
;[
6-
'index.js',
7-
'lib/source-map-cache.js'
6+
'index.js'
87
].forEach(function (name) {
98
var indexPath = path.join(__dirname, name)
109
var source = fs.readFileSync(indexPath, 'utf8')

0 commit comments

Comments
 (0)