Skip to content

Commit 70a4095

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

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

docs/recipes/code-coverage.md

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

0 commit comments

Comments
 (0)