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 |
|
10 | Istanbul'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 |
|
17 | You can install nyc as a development dependency and add it to the test stanza
|
18 | in your package.json.
|
19 |
|
20 | ```shell
|
21 | npm i nyc --save-dev
|
22 | ```
|
23 |
|
24 | ```json
|
25 | {
|
26 | "script": {
|
27 | "test": "nyc tap ./test/*.js"
|
28 | }
|
29 | }
|
30 | ```
|
31 |
|
32 | Alternatively, you can install nyc globally and use it to execute `npm test`:
|
33 |
|
34 | ```shell
|
35 | npm i nyc -g
|
36 | ```
|
37 |
|
38 | ```shell
|
39 | nyc npm test
|
40 | ```
|
41 |
|
42 | nyc accepts a wide variety of configuration arguments, run `nyc --help` for
|
43 | thorough documentation.
|
44 |
|
45 | Configuration arguments should be provided prior to the program that nyc
|
46 | is executing. As an example, the following command executes `npm test`,
|
47 | and indicates to nyc that it should output both an `lcov`
|
48 | and a `text-lcov` coverage report.
|
49 |
|
50 | ```shell
|
51 | nyc --reporter=lcov --reporter=text-lcov npm test
|
52 | ```
|
53 |
|
54 | ## Support for custom require hooks (babel, webpack, etc.)
|
55 |
|
56 | nyc supports custom require hooks like
|
57 | [`babel-register`](http://babeljs.io/docs/usage/require/). nyc can
|
58 | load the hooks for you, [using the `--require`
|
59 | flag](#require-additional-modules).
|
60 |
|
61 | Source maps are used to map coverage information back to the appropriate lines
|
62 | of the pre-transpiled code. You'll have to configure your custom require hook
|
63 | to inline the source map in the transpiled code. For Babel that means setting
|
64 | the `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 |
|
70 | 1. 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 |
|
90 | 2. 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 |
|
107 | That'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 |
|
113 | Supporting file extensions can be configured through either the configuration arguments or with the `nyc` config section in `package.json`.
|
114 |
|
115 | ```shell
|
116 | nyc --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 |
|
132 | nyc can fail tests if coverage falls below a threshold.
|
133 | After running your tests with nyc, simply run:
|
134 |
|
135 | ```shell
|
136 | nyc check-coverage --lines 95 --functions 95 --branches 95
|
137 | ```
|
138 |
|
139 | nyc also accepts a `--check-coverage` shorthand, which can be used to
|
140 | both run tests and check that coverage falls within the threshold provided:
|
141 |
|
142 | ```shell
|
143 | nyc --check-coverage --lines 100 npm test
|
144 | ```
|
145 |
|
146 | The above check fails if coverage falls below 100%.
|
147 |
|
148 | ## Running reports
|
149 |
|
150 | Once you've run your tests with nyc, simply run:
|
151 |
|
152 | ```bash
|
153 | nyc report
|
154 | ```
|
155 |
|
156 | To view your coverage report:
|
157 |
|
158 | <img width="500" src="screen.png">
|
159 |
|
160 | you can use any reporters that are supported by istanbul:
|
161 |
|
162 | ```bash
|
163 | nyc report --reporter=lcov
|
164 | ```
|
165 |
|
166 | ## Excluding files
|
167 |
|
168 | You can tell nyc to exclude specific files and directories by adding
|
169 | an `nyc.exclude` array to your `package.json`. Each element of
|
170 | the array is a glob pattern indicating which paths should be omitted.
|
171 |
|
172 | Globs are matched using [micromatch](https://www.npmjs.com/package/micromatch).
|
173 |
|
174 | For example, the following config will exclude any files with the extension `.spec.js`,
|
175 | and 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`
|
188 | folder 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__/**']`,
|
191 | which would exclude `test`/`__tests__` directories as well as `test.js`, `*.test.js`,
|
192 | and `test-*.js` files. Specifying your own exclude property overrides these defaults.
|
193 |
|
194 | ## Including files
|
195 |
|
196 | As an alternative to providing a list of files to `exclude`, you can provide
|
197 | an `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 |
|
211 | By default nyc does not collect coverage for files that have not
|
212 | been required, run nyc with the flag `--all` to enable this.
|
213 |
|
214 | ## Require additional modules
|
215 |
|
216 | The `--require` flag can be provided to `nyc` to indicate that additional
|
217 | modules should be required in the subprocess collecting coverage:
|
218 |
|
219 | `nyc --require babel-register --require babel-polyfill mocha`
|
220 |
|
221 | ## Caching
|
222 |
|
223 | You can run `nyc` with the optional `--cache` flag, to prevent it from
|
224 | instrumenting the same files multiple times. This can significantly
|
225 | improve runtime performance.
|
226 |
|
227 | ## Configuring `nyc`
|
228 |
|
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:**
|
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 |
|
281 | nyc's `instrument` command can be used to instrument
|
282 | source 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 |
|
290 | nyc is able to show you all Node processes that are spawned when running a
|
291 | test script under it:
|
292 |
|
293 | ```
|
294 | $ nyc --show-process-tree npm test
|
295 | 3 passed
|
296 | ----------|----------|----------|----------|----------|----------------|
|
297 | File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
|
298 | ----------|----------|----------|----------|----------|----------------|
|
299 | All files | 100 | 100 | 100 | 100 | |
|
300 | index.js | 100 | 100 | 100 | 100 | |
|
301 | ----------|----------|----------|----------|----------|----------------|
|
302 | nyc
|
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
|
311 | coverage reports to your GitHub project. Here's how to get nyc
|
312 | integrated with coveralls and travis-ci.org:
|
313 |
|
314 | 1. add the coveralls and nyc dependencies to your module:
|
315 |
|
316 | ```shell
|
317 | npm install coveralls nyc --save
|
318 | ```
|
319 |
|
320 | 2. 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 |
|
331 | 3. For private repos, add the environment variable `COVERALLS_REPO_TOKEN` to travis.
|
332 |
|
333 | 4. add the following to your `.travis.yml`:
|
334 |
|
335 | ```yaml
|
336 | after_success: npm run coverage
|
337 | ```
|
338 |
|
339 | That'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
|
348 | coverage reports to your GitHub project, even viewing them inline on GitHub with a browser extension:
|
349 |
|
350 | Here's how to get `nyc` integrated with codecov and travis-ci.org:
|
351 |
|
352 | 1. add the codecov and nyc dependencies to your module:
|
353 |
|
354 | ```shell
|
355 | npm install codecov nyc --save-dev
|
356 | ```
|
357 |
|
358 | 2. 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 |
|
369 | 3. For private repos, add the environment variable `CODECOV_TOKEN` to travis.
|
370 |
|
371 | 4. add the following to your `.travis.yml`:
|
372 |
|
373 | ```yaml
|
374 | after_success: npm run coverage
|
375 | ```
|
376 |
|
377 | That's all there is to it!
|
378 |
|
\ | No newline at end of file |