UNPKG

6.43 kBMarkdownView Raw
1# nyc
2
3[![Build Status](https://travis-ci.org/bcoe/nyc.png)](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=)
5[![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)
7[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
8
9a code coverage tool built on [istanbul](https://www.npmjs.com/package/istanbul)
10that works for applications that spawn subprocesses.
11
12## Instrumenting Your Code
13
14You can install nyc as a development dependency and add it to the test stanza
15in your package.json.
16
17```shell
18npm i nyc --save-dev
19```
20
21```json
22{
23 "script": {
24 "test": "nyc tap ./test/*.js"
25 }
26}
27```
28
29Alternatively, you can install nyc globally and use it to execute `npm test`:
30
31```shell
32npm i nyc -g
33```
34
35```shell
36nyc npm test
37```
38
39nyc accepts a wide variety of configuration arguments, run `nyc --help` for
40thorough documentation.
41
42Configuration arguments should be provided prior to the program that nyc
43is executing. As an example, the following command executes `npm test`,
44and indicates to nyc that it should output both an `lcov`
45and a `text-lcov` coverage report.
46
47```shell
48nyc --reporter=lcov --reporter=text-lcov npm test
49```
50
51## Support For Custom Require Hooks (Babel! ES2015!)
52
53nyc supports custom require hooks like
54[`babel-register`](http://babeljs.io/docs/usage/require/). If necessary nyc can
55load the hooks for you, [using the `--require`
56flag](#require-additional-modules).
57
58Source maps are used to map coverage information back to the appropriate lines
59of the pre-transpiled code. You'll have to configure your custom require hook
60to inline the source map in the transpiled code. For Babel that means setting
61the `sourceMaps` option to `inline`.
62
63## Support For Custom File Extensions (.jsx, .es6)
64
65Supporting file extensions can be configured through either the configuration arguments or with the `nyc` config section in `package.json`.
66
67```shell
68nyc --extension .jsx --extension .es6 npm test
69```
70
71```json
72{
73 "nyc": {
74 "extension": [
75 ".jsx",
76 ".es6"
77 ]
78 }
79}
80```
81
82## Checking Coverage
83
84nyc exposes istanbul's check-coverage tool. After running your tests with nyc,
85simply run:
86
87```shell
88nyc check-coverage --lines 95 --functions 95 --branches 95
89```
90
91This feature makes it easy to fail your tests if coverage drops below a given threshold.
92
93nyc also accepts a `--check-coverage` shorthand, which can be used to
94both run tests and check that coverage falls within the threshold provided:
95
96```shell
97nyc --check-coverage --lines 100 npm test
98```
99
100The above check fails if coverage falls below 100%.
101
102## Running Reports
103
104Once you've run your tests with nyc, simply run:
105
106```bash
107nyc report
108```
109
110To view your coverage report:
111
112<img width="500" src="screen.png">
113
114you can use any reporters that are supported by istanbul:
115
116```bash
117nyc report --reporter=lcov
118```
119
120## Excluding Files
121
122You can tell nyc to exclude specific files and directories by adding
123an `nyc.exclude` array to your `package.json`. Each element of
124the array is a glob pattern indicating which paths should be omitted.
125
126Globs are matched using [micromatch](https://www.npmjs.com/package/micromatch).
127
128In addition to patterns specified in the package, nyc will always exclude
129files in `node_modules`.
130
131For example, the following config will exclude everything in `node_modules`,
132any files with the extension `.spec.js`, and anything in the `build`
133directory:
134
135```json
136{
137 "nyc": {
138 "exclude": [
139 "**/*.spec.js",
140 "build"
141 ]
142 }
143}
144```
145
146> Note: exclude defaults to `['test', 'test{,-*}.js', '**/*.test.js', '**/__tests__/**']`,
147which would exclude `test`/`__tests__` directories as well as `test.js`, `*.test.js`,
148and `test-*.js` files. Specifying your own exclude property overrides these defaults.
149
150## Including Files
151
152As an alternative to providing a list of files to `exclude`, you can provide
153an `include` key to specify specific files that should be covered:
154
155```json
156{
157 "nyc": {
158 "include": ["**/build/umd/moment.js"]
159 }
160}
161```
162
163> Note: include defaults to `['**']`
164
165## Include Reports For Files That Are Not Required
166
167By default nyc does not collect coverage for files that have not
168been required, run nyc with the flag `--all` to enable this.
169
170## Require additional modules
171
172The `--require` flag can be provided to `nyc` to indicate that additional
173modules should be required in the subprocess collecting coverage:
174
175`nyc --require babel-core/register --require babel-polyfill mocha`
176
177## Caching
178
179You can run `nyc` with the optional `--cache` flag, to prevent it from
180instrumenting the same files multiple times. This can signficantly
181improve runtime performance.
182
183## Configuring `nyc`
184
185Any configuration options that can be set via the command line
186can also be specified in the `nyc` stanza of your package.json:
187
188```json
189{
190 "nyc": {
191 "lines": 99,
192 "check-coverage": false,
193 "report-dir": "./alternative"
194 }
195}
196```
197
198## Configuring Istanbul
199
200Behind the scenes nyc uses [istanbul](https://www.npmjs.com/package/istanbul). You
201can place a `.istanbul.yml` file in your project's root directory to pass config
202setings to istanbul's code instrumenter:
203
204```yml
205instrumentation:
206 preserve-comments: true
207```
208
209## Integrating With Coveralls
210
211[coveralls.io](https://coveralls.io) is a great tool for adding
212coverage reports to your GitHub project. Here's how to get nyc
213integrated with coveralls and travis-ci.org:
214
2151. add the coveralls and nyc dependencies to your module:
216
217 ```shell
218 npm install coveralls nyc --save
219 ```
220
2212. update the scripts in your package.json to include these bins:
222
223 ```json
224 {
225 "script": {
226 "test": "nyc tap ./test/*.js",
227 "coverage": "nyc report --reporter=text-lcov | coveralls"
228 }
229 }
230 ```
231
2323. For private repos, add the environment variable `COVERALLS_REPO_TOKEN` to travis.
233
2344. add the following to your `.travis.yml`:
235
236 ```yaml
237 after_success: npm run coverage
238 ```
239
240That's all there is to it!
241
242> 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?`.
243
\No newline at end of file