Skip to content

Commit af2c2b6

Browse files
committed
Simplify code coverage recipe
nyc is now officially part of Istanbul. They also have a tutorial on how to integrate it with Babel. We should simplify the recipe so it's only about using nyc with AVA. I've left the .gitignore bit, but removed Coveralls since it's not directly related to AVA.
1 parent 349648a commit af2c2b6

File tree

1 file changed

+10
-160
lines changed

1 file changed

+10
-160
lines changed

docs/recipes/code-coverage.md

Lines changed: 10 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
1-
> **Please note, this recipe has not yet been updated for Babel 7 support in AVA 1.0.**
2-
3-
---
4-
51
# Code coverage
62

73
Translations: [Español](https://github.com/avajs/ava-docs/blob/master/es_ES/docs/recipes/code-coverage.md), [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/code-coverage.md), [Italiano](https://github.com/avajs/ava-docs/blob/master/it_IT/docs/recipes/code-coverage.md), [日本語](https://github.com/avajs/ava-docs/blob/master/ja_JP/docs/recipes/code-coverage.md), [Português](https://github.com/avajs/ava-docs/blob/master/pt_BR/docs/recipes/code-coverage.md), [Русский](https://github.com/avajs/ava-docs/blob/master/ru_RU/docs/recipes/code-coverage.md), [简体中文](https://github.com/avajs/ava-docs/blob/master/zh_CN/docs/recipes/code-coverage.md)
84

9-
As AVA [spawns the test files][process-isolation], you can't use [`istanbul`] for code coverage; instead, you can achieve this with [`nyc`] which is basically [`istanbul`] with sub-process support.
10-
11-
## Setup
5+
Use the [nyc] command-line-client for [Istanbul] to compute the code coverage of your tests.
126

13-
First install NYC:
7+
First install [nyc]:
148

159
```
16-
$ npm install nyc --save-dev
10+
$ npm install --save-dev nyc
1711
```
1812

19-
Then add both the `.nyc_output` and `coverage` directories to your `.gitignore` file.
20-
21-
`.gitignore`:
22-
23-
```
24-
node_modules
25-
coverage
26-
.nyc_output
27-
```
28-
29-
## ES5 coverage
30-
31-
Using NYC to provide coverage for production code written in ES5 is simple. Just prepend your test script with `nyc`:
13+
At its simplest run AVA through [nyc]. In your `package.json` file:
3214

3315
```json
3416
{
@@ -38,146 +20,14 @@ Using NYC to provide coverage for production code written in ES5 is simple. Just
3820
}
3921
```
4022

41-
That's it!
23+
You may want to exclude the `.nyc_output` and `coverage` directories from source control. Assuming you're using Git, add the following to your `.gitignore` file:
4224

43-
If you want to create HTML coverage reports, or upload coverage data to Coveralls, you should skip down to those sections below.
44-
45-
## ES2015 coverage
46-
47-
Using Babel to transpile your production code is a bit more involved. Here we've broken it down into multiple steps.
48-
49-
### Configure Babel
50-
51-
First, we need a Babel configuration. The following is just an example. You will need to modify it to fit your needs.
52-
53-
`package.json`:
54-
```json
55-
{
56-
"babel": {
57-
"presets": ["es2015"],
58-
"plugins": ["transform-runtime"],
59-
"ignore": "test.js",
60-
"env": {
61-
"development": {
62-
"sourceMaps": "inline"
63-
}
64-
}
65-
}
66-
}
6725
```
68-
69-
There are two important things to note from the example above.
70-
71-
1. We ignore test files because AVA already handles transpiling tests for you.
72-
73-
2. We specify `inline` source maps for development. This is important for properly generating coverage. Using the `env` section of the Babel configuration allows us to disable source maps for production builds.
74-
75-
76-
### Create a build script
77-
78-
Since it is unlikely you want `inline` source maps in your production code. You should specify an alternate environment variable in your build scripts:
79-
80-
`package.json`
81-
82-
```json
83-
{
84-
"scripts": {
85-
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
86-
}
87-
}
88-
```
89-
90-
> WARNING: `BABEL_ENV=production` does not work on Windows, you must use the `set` keyword (`set BABEL_ENV=production`). For cross platform builds, check out [`cross-env`].
91-
92-
Note that the build script really has very little to do with AVA, and is just a demonstration of how to use Babel's `env` configuration to manipulate your config so it's compatible with AVA.
93-
94-
### Use the Babel require hook
95-
96-
To use the Babel require hook, add `babel-core/register` to the `require` section of you AVA config in `package.json`.
97-
98-
```json
99-
{
100-
"ava": {
101-
"require": ["babel-core/register"]
102-
}
103-
}
104-
```
105-
106-
### Putting it all together
107-
108-
Combining the above steps, your complete `package.json` should look something like this:
109-
110-
```json
111-
{
112-
"scripts": {
113-
"test": "nyc ava",
114-
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
115-
},
116-
"babel": {
117-
"presets": ["es2015"],
118-
"plugins": ["transform-runtime"],
119-
"ignore": "test.js",
120-
"env": {
121-
"development": {
122-
"sourceMaps": "inline"
123-
}
124-
}
125-
},
126-
"ava": {
127-
"require": ["babel-core/register"]
128-
}
129-
}
130-
```
131-
132-
133-
## HTML reports
134-
135-
NYC creates a `json` coverage file for each forked process in the `.nyc_ouput` directory.
136-
137-
To combine those into a human readable HTML report, do the following:
138-
139-
```
140-
$ ./node_modules/.bin/nyc report --reporter=html
141-
```
142-
143-
Or, use an npm script to save on typing:
144-
145-
```json
146-
{
147-
"scripts": {
148-
"report": "nyc report --reporter=html"
149-
}
150-
}
151-
```
152-
153-
This will output a HTML file to the `coverage` directory.
154-
155-
156-
## Hosted coverage
157-
158-
### Travis CI & Coveralls
159-
160-
First, you must login to [coveralls.io] and activate your repository.
161-
162-
Once that is done, add [`coveralls`] as a development dependency:
163-
164-
```
165-
$ npm install coveralls --save-dev
166-
```
167-
168-
Then add the following to your `.travis.yml`:
169-
170-
```yaml
171-
after_success:
172-
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
26+
.nyc_output
27+
coverage
17328
```
17429

175-
Your coverage report will then appear on coveralls shortly after Travis completes.
30+
If you're compiling your source files using Babel you may want to apply Istanbul's instrumentation as part of the source file compilation. This will yield better results than instrumenting Babel's output. See Istanbul's [*Using Istanbul With ES2015+* tutorial](https://istanbul.js.org/docs/tutorials/es2015/). AVA sets `NODE_ENV=test` for you. Note that as of February 2018 this tutorial hasn't yet been updated for Babel 7.
17631

177-
[`babel`]: https://github.com/babel/babel
178-
[coveralls.io]: https://coveralls.io
179-
[`coveralls`]: https://github.com/nickmerwin/node-coveralls
180-
[`cross-env`]: https://github.com/kentcdodds/cross-env
181-
[process-isolation]: https://github.com/avajs/ava#process-isolation
182-
[`istanbul`]: https://github.com/gotwarlost/istanbul
183-
[`nyc`]: https://github.com/bcoe/nyc
32+
[Istanbul]: https://istanbul.js.org/
33+
[nyc]: https://github.com/istanbuljs/nyc

0 commit comments

Comments
 (0)