Skip to content

Commit a9d282c

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

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

docs/recipes/code-coverage.md

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
To cover ES6, simply prepend your test script with `nyc` and the `--babel` flag.
34+
We'll use the `text` reporter to give us a coverage report log after we run the
35+
tests, and the `lcov` reporter so that we can push the results to a hosted
36+
code coverage platform. This npm script will then handle our code coverage
37+
and testing:
38+
39+
```json
40+
{
41+
"scripts": {
42+
"test": "nyc --babel --reporter=lcov --reporter=text ava test.js"
43+
}
44+
}
45+
```
46+
47+
48+
## ES5 coverage
49+
50+
To cover ES5, simply prepend your test script with `nyc`. We'll use the `text`
51+
reporter to give us a coverage report log after we run the tests, and the `lcov`
52+
reporter so that we can push the results to a hosted code coverage platform.
53+
This npm script will then handle our code coverage and testing:
54+
55+
```json
56+
{
57+
"scripts": {
58+
"test": "nyc --reporter=lcov --reporter=text ava test.js"
59+
}
60+
}
61+
```
62+
63+
64+
## HTML reports
65+
66+
To see a HTML report for either the ES6 or ES5 coverage strategies we have
67+
outlined, do:
68+
69+
```
70+
nyc report --reporter html
71+
```
72+
73+
Or, convert it into an npm script for less typing:
74+
75+
```json
76+
{
77+
"scripts": {
78+
"report": "nyc report --reporter html"
79+
}
80+
}
81+
```
82+
83+
This will output a HTML file to the `coverage` directory.
84+
85+
86+
## Hosted coverage
87+
88+
### Travis CI & Coveralls
89+
90+
Add [`coveralls`] as a development dependency:
91+
92+
```
93+
npm i coveralls --save-dev
94+
```
95+
96+
Then add the following to your `.travis.yml`:
97+
98+
```
99+
after_script:
100+
- 'cat coverage/lcov.info | ./node_modules/.bin/coveralls'
101+
```
102+
103+
Your coverage report will then appear on coveralls shortly after the CI
104+
service completes.
105+
106+
[`babel`]: https://github.com/babel/babel
107+
[`coveralls`]: https://github.com/nickmerwin/node-coveralls
108+
[isolated-env]: https://github.com/sindresorhus/ava#isolated-environment
109+
[`istanbul`]: https://github.com/gotwarlost/istanbul
110+
[`nyc`]: https://github.com/bcoe/nyc

0 commit comments

Comments
 (0)