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