Skip to content

Commit 10f9dd1

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

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

docs/recipes/code-coverage.md

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

0 commit comments

Comments
 (0)