UNPKG

10.1 kBMarkdownView Raw
1# nyc
2
3[![Join the chat at https://gitter.im/istanbuljs/nyc](https://badges.gitter.im/istanbuljs/nyc.svg)](https://gitter.im/istanbuljs/nyc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4[![Build Status](https://travis-ci.org/istanbuljs/nyc.svg?branch=master)](https://travis-ci.org/istanbuljs/nyc)
5[![Coverage Status](https://coveralls.io/repos/bcoe/nyc/badge.svg?branch=)](https://coveralls.io/r/bcoe/nyc?branch=master)
6[![NPM version](https://img.shields.io/npm/v/nyc.svg)](https://www.npmjs.com/package/nyc)
7[![Windows Tests](https://img.shields.io/appveyor/ci/bcoe/nyc-ilw23/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/bcoe/nyc-ilw23)
8[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
9
10Istanbul's state of the art command line interface, with support for:
11
12* applications that spawn subprocesses.
13* ES2015 transforms, via [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul), or source-maps.
14
15## Instrumenting your code
16
17You can install nyc as a development dependency and add it to the test stanza
18in your package.json.
19
20```shell
21npm i nyc --save-dev
22```
23
24```json
25{
26 "script": {
27 "test": "nyc tap ./test/*.js"
28 }
29}
30```
31
32Alternatively, you can install nyc globally and use it to execute `npm test`:
33
34```shell
35npm i nyc -g
36```
37
38```shell
39nyc npm test
40```
41
42nyc accepts a wide variety of configuration arguments, run `nyc --help` for
43thorough documentation.
44
45Configuration arguments should be provided prior to the program that nyc
46is executing. As an example, the following command executes `npm test`,
47and indicates to nyc that it should output both an `lcov`
48and a `text-lcov` coverage report.
49
50```shell
51nyc --reporter=lcov --reporter=text-lcov npm test
52```
53
54## Support for custom require hooks (babel, webpack, etc.)
55
56nyc supports custom require hooks like
57[`babel-register`](http://babeljs.io/docs/usage/require/). nyc can
58load the hooks for you, [using the `--require`
59flag](#require-additional-modules).
60
61Source maps are used to map coverage information back to the appropriate lines
62of the pre-transpiled code. You'll have to configure your custom require hook
63to inline the source map in the transpiled code. For Babel that means setting
64the `sourceMaps` option to `inline`.
65
66## Use with babel-plugin-istanbul for ES6/ES7/ES2015 Support
67
68[`babel-plugin-istanbul`](https://github.com/istanbuljs/babel-plugin-istanbul) can be used to enable better first-class ES6 support.
69
701. enable the `babel-plugin-istanbul` plugin:
71
72 ```json
73 {
74 "babel": {
75 "presets": ["es2015"],
76 "env": {
77 "test": {
78 "plugins": ["istanbul"]
79 }
80 }
81 }
82 }
83 ```
84
85 Note: With this configuration, the Istanbul instrumentation will only be active when `NODE_ENV` or `BABEL_ENV` is `test`.
86
87 We recommend using the [`cross-env`](https://npmjs.com/package/cross-env) package to set these environment variables
88 in your `package.json` scripts in a way that works cross-platform.
89
902. disable nyc's instrumentation and source-maps:
91
92 ```json
93 {
94 "nyc": {
95 "include": [
96 "src/*.js"
97 ],
98 "require": [
99 "babel-register"
100 ],
101 "sourceMap": false,
102 "instrument": false
103 }
104 }
105 ```
106
107That's all there is to it, better ES6 syntax highlighting awaits:
108
109<img width="500" src="screen2.png">
110
111## Support for alternate file extensions (.jsx, .es6)
112
113Supporting file extensions can be configured through either the configuration arguments or with the `nyc` config section in `package.json`.
114
115```shell
116nyc --extension .jsx --extension .es6 npm test
117```
118
119```json
120{
121 "nyc": {
122 "extension": [
123 ".jsx",
124 ".es6"
125 ]
126 }
127}
128```
129
130## Checking coverage
131
132nyc can fail tests if coverage falls below a threshold.
133After running your tests with nyc, simply run:
134
135```shell
136nyc check-coverage --lines 95 --functions 95 --branches 95
137```
138
139nyc also accepts a `--check-coverage` shorthand, which can be used to
140both run tests and check that coverage falls within the threshold provided:
141
142```shell
143nyc --check-coverage --lines 100 npm test
144```
145
146The above check fails if coverage falls below 100%.
147
148## Running reports
149
150Once you've run your tests with nyc, simply run:
151
152```bash
153nyc report
154```
155
156To view your coverage report:
157
158<img width="500" src="screen.png">
159
160you can use any reporters that are supported by istanbul:
161
162```bash
163nyc report --reporter=lcov
164```
165
166## Excluding files
167
168You can tell nyc to exclude specific files and directories by adding
169an `nyc.exclude` array to your `package.json`. Each element of
170the array is a glob pattern indicating which paths should be omitted.
171
172Globs are matched using [micromatch](https://www.npmjs.com/package/micromatch).
173
174For example, the following config will exclude any files with the extension `.spec.js`,
175and anything in the `build` directory:
176
177```json
178{
179 "nyc": {
180 "exclude": [
181 "**/*.spec.js",
182 "build"
183 ]
184 }
185}
186```
187> Note: Since version 8.0 if you add a "exclude" array the `node_modules`
188folder is not automatically excluded, you will need to explicitly add it to your exclude array
189
190> Note: exclude defaults to `['test', 'test{,-*}.js', '**/*.test.js', '**/__tests__/**']`,
191which would exclude `test`/`__tests__` directories as well as `test.js`, `*.test.js`,
192and `test-*.js` files. Specifying your own exclude property overrides these defaults.
193
194## Including files
195
196As an alternative to providing a list of files to `exclude`, you can provide
197an `include` key to specify specific files that should be covered:
198
199```json
200{
201 "nyc": {
202 "include": ["**/build/umd/moment.js"]
203 }
204}
205```
206
207> Note: include defaults to `['**']`
208
209## Include reports for files that are not required
210
211By default nyc does not collect coverage for files that have not
212been required, run nyc with the flag `--all` to enable this.
213
214## Require additional modules
215
216The `--require` flag can be provided to `nyc` to indicate that additional
217modules should be required in the subprocess collecting coverage:
218
219`nyc --require babel-register --require babel-polyfill mocha`
220
221## Caching
222
223You can run `nyc` with the optional `--cache` flag, to prevent it from
224instrumenting the same files multiple times. This can significantly
225improve runtime performance.
226
227## Configuring `nyc`
228
229Any 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:**
232
233```json
234{
235 "description": "These are just examples for demonstration, nothing prescriptive",
236 "nyc": {
237 "lines": 99,
238 "statements": 99,
239 "functions": 99,
240 "branches": 99,
241 "include": [
242 "src/**/*.js"
243 ],
244 "exclude": [
245 "src/**/*.spec.js"
246 ],
247 "reporter": [
248 "lcov",
249 "text-summary"
250 ],
251 "require": [
252 "./test/helpers/some-helper.js"
253 ],
254 "extension": [
255 ".jsx"
256 ],
257 "cache": true,
258 "all": true,
259 "check-coverage": true,
260 "report-dir": "./alternative"
261 }
262}
263```
264
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
279## Instrumenting source files
280
281nyc's `instrument` command can be used to instrument
282source files outside of the context of your unit-tests:
283
284__instrument the entire ./lib folder:__
285
286`nyc instrument ./lib ./output`
287
288## Process tree information
289
290nyc is able to show you all Node processes that are spawned when running a
291test script under it:
292
293```
294$ nyc --show-process-tree npm test
295 3 passed
296----------|----------|----------|----------|----------|----------------|
297File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
298----------|----------|----------|----------|----------|----------------|
299All files | 100 | 100 | 100 | 100 | |
300 index.js | 100 | 100 | 100 | 100 | |
301----------|----------|----------|----------|----------|----------------|
302nyc
303└─┬ /usr/local/bin/node /usr/local/bin/npm test
304 └─┬ /usr/local/bin/node /path/to/your/project/node_modules/.bin/ava
305 └── /usr/local/bin/node /path/to/your/project/node_modules/ava/lib/test-worker.js …
306```
307
308## Integrating with coveralls
309
310[coveralls.io](https://coveralls.io) is a great tool for adding
311coverage reports to your GitHub project. Here's how to get nyc
312integrated with coveralls and travis-ci.org:
313
3141. add the coveralls and nyc dependencies to your module:
315
316 ```shell
317 npm install coveralls nyc --save
318 ```
319
3202. update the scripts in your package.json to include these bins:
321
322 ```json
323 {
324 "script": {
325 "test": "nyc tap ./test/*.js",
326 "coverage": "nyc report --reporter=text-lcov | coveralls"
327 }
328 }
329 ```
330
3313. For private repos, add the environment variable `COVERALLS_REPO_TOKEN` to travis.
332
3334. add the following to your `.travis.yml`:
334
335 ```yaml
336 after_success: npm run coverage
337 ```
338
339That's all there is to it!
340
341> 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?`.
342
343## Integrating with codecov
344
345`nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov`
346
347[codecov](https://codecov.io/) is a great tool for adding
348coverage reports to your GitHub project, even viewing them inline on GitHub with a browser extension:
349
350Here's how to get `nyc` integrated with codecov and travis-ci.org:
351
3521. add the codecov and nyc dependencies to your module:
353
354 ```shell
355 npm install codecov nyc --save-dev
356 ```
357
3582. update the scripts in your package.json to include these bins:
359
360 ```json
361 {
362 "script": {
363 "test": "nyc tap ./test/*.js",
364 "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
365 }
366 }
367 ```
368
3693. For private repos, add the environment variable `CODECOV_TOKEN` to travis.
370
3714. add the following to your `.travis.yml`:
372
373 ```yaml
374 after_success: npm run coverage
375 ```
376
377That's all there is to it!
378
\No newline at end of file