Skip to content

Commit 858d3d0

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

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

docs/recipes/code-coverage.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Code coverage
2+
3+
As AVA [spawns the test files][isolated-env], you can't use [`istanbul`] for code coverage; instead, you can achieve this with [`nyc`] which is basically [`istanbul`] with sub-process support. So, firstly we'll need to install it:
4+
5+
```
6+
npm install nyc --save-dev
7+
```
8+
9+
For both ES2015 and ES5 environments, don't forget to add `.nyc_output` & `coverage` to your `.gitignore`.
10+
11+
12+
## ES5 coverage
13+
14+
To cover ES5, simply prepend your test script with `nyc`. This npm script will then handle our code coverage and testing:
15+
16+
```json
17+
{
18+
"scripts": {
19+
"test": "nyc ava"
20+
}
21+
}
22+
```
23+
24+
25+
## ES2015 coverage
26+
27+
First, we'll need a babel configuration. This will vary from developer to developer but you can use this `package.json` configuration for babel as a starting point:
28+
29+
```json
30+
{
31+
"babel": {
32+
"presets": ["es2015"],
33+
"plugins": ["transform-runtime"],
34+
"ignore": "test.js",
35+
"env": {
36+
"development": {
37+
"sourceMaps": "inline"
38+
}
39+
}
40+
}
41+
}
42+
```
43+
44+
Note that in development mode, we need to specify a sourcemap when we transpile our code, and in production this is unnecessary. So for your production script, use an environment other than development; for example:
45+
46+
```json
47+
{
48+
"scripts": {
49+
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
50+
}
51+
}
52+
```
53+
54+
To cover ES6, simply prepend your test script with `nyc` and the `--babel` flag. This npm script will then handle our code coverage and testing:
55+
56+
```json
57+
{
58+
"scripts": {
59+
"test": "nyc --babel --reporter=text ava"
60+
}
61+
}
62+
```
63+
64+
65+
## HTML reports
66+
67+
To see a HTML report for either the ES6 or ES5 coverage strategies we have 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+
Firstly, you will need to activate your repository in the coveralls user interface. Once that is done, add [`coveralls`] as a development dependency:
91+
92+
```
93+
npm install coveralls --save-dev
94+
```
95+
96+
Then add the following to your `.travis.yml`:
97+
98+
```
99+
after_success:
100+
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
101+
```
102+
103+
Your coverage report will then appear on coveralls shortly after the CI service completes.
104+
105+
[`babel`]: https://github.com/babel/babel
106+
[`coveralls`]: https://github.com/nickmerwin/node-coveralls
107+
[isolated-env]: https://github.com/sindresorhus/ava#isolated-environment
108+
[`istanbul`]: https://github.com/gotwarlost/istanbul
109+
[`nyc`]: https://github.com/bcoe/nyc

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ Concurrency is not parallelism. It enables parallelism. [Learn more.](http://sta
715715

716716
## Recipes
717717

718+
- [Code coverage](docs/recipes/code-coverage.md)
718719
- [Endpoint testing](docs/recipes/endpoint-testing.md)
719720

720721

0 commit comments

Comments
 (0)