Skip to content

Commit 5985784

Browse files
committed
Add a code coverage guide.
1 parent 175ca41 commit 5985784

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

docs/recipes/code-coverage.md

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Code coverage
2+
3+
As AVA [spawns the test files][isolated-env], you can't use [`istanbul`] for
4+
code coverage; instead, you can achieve this with [`nyc`] which is basically
5+
[`istanbul`] with sub-process support. So, firstly we'll need to install it:
6+
7+
```
8+
npm install nyc --save-dev
9+
```
10+
11+
For both ES2015 and ES5 environments, don't forget to add `.nyc_output` &
12+
`coverage` to your `.gitignore`.
13+
14+
15+
## ES5 coverage
16+
17+
To cover ES5, simply prepend your test script with `nyc`. We'll use the `text`
18+
reporter to give us a coverage report log after we run the tests, and the `lcov`
19+
reporter so that we can push the results to a hosted code coverage platform.
20+
This npm script will then handle our code coverage and testing:
21+
22+
```json
23+
{
24+
"scripts": {
25+
"test": "nyc --reporter=lcov --reporter=text ava test.js"
26+
}
27+
}
28+
```
29+
30+
31+
## ES2015 coverage
32+
33+
First, we'll need a babel configuration. This will vary from developer to
34+
developer but you can use this `package.json` configuration for babel as a
35+
starting point:
36+
37+
```json
38+
{
39+
"babel": {
40+
"presets": ["es2015"],
41+
"plugins": ["transform-runtime"],
42+
"ignore": "test.js",
43+
"env": {
44+
"development": {
45+
"sourceMaps": "inline"
46+
}
47+
}
48+
}
49+
}
50+
```
51+
52+
Note that in development mode, we need to specify a sourcemap when we transpile
53+
our code, and in production this is unnecessary. So for your production script,
54+
use an environment other than development; for example:
55+
56+
```json
57+
{
58+
"scripts": {
59+
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
60+
}
61+
}
62+
```
63+
64+
To cover ES6, simply prepend your test script with `nyc` and the `--babel` flag.
65+
We'll use the `text` reporter to give us a coverage report log after we run the
66+
tests, and the `lcov` reporter so that we can push the results to a hosted
67+
code coverage platform. This npm script will then handle our code coverage
68+
and testing:
69+
70+
```json
71+
{
72+
"scripts": {
73+
"test": "nyc --babel --reporter=lcov --reporter=text ava test.js"
74+
}
75+
}
76+
```
77+
78+
79+
## HTML reports
80+
81+
To see a HTML report for either the ES6 or ES5 coverage strategies we have
82+
outlined, do:
83+
84+
```
85+
nyc report --reporter html
86+
```
87+
88+
Or, convert it into an npm script for less typing:
89+
90+
```json
91+
{
92+
"scripts": {
93+
"report": "nyc report --reporter html"
94+
}
95+
}
96+
```
97+
98+
This will output a HTML file to the `coverage` directory.
99+
100+
101+
## Hosted coverage
102+
103+
### Travis CI & Coveralls
104+
105+
Add [`coveralls`] as a development dependency:
106+
107+
```
108+
npm install coveralls --save-dev
109+
```
110+
111+
Then add the following to your `.travis.yml`:
112+
113+
```
114+
after_script:
115+
- 'cat coverage/lcov.info | ./node_modules/.bin/coveralls'
116+
```
117+
118+
Your coverage report will then appear on coveralls shortly after the CI
119+
service completes.
120+
121+
[`babel`]: https://github.com/babel/babel
122+
[`coveralls`]: https://github.com/nickmerwin/node-coveralls
123+
[isolated-env]: https://github.com/sindresorhus/ava#isolated-environment
124+
[`istanbul`]: https://github.com/gotwarlost/istanbul
125+
[`nyc`]: https://github.com/bcoe/nyc

0 commit comments

Comments
 (0)