Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a code coverage recipe #444

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions docs/recipes/code-coverage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Code coverage

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:

```
npm install nyc --save-dev
```

For both ES2015 and ES5 environments, don't forget to add `.nyc_output` & `coverage` to your `.gitignore`.


## ES5 coverage

To cover ES5, simply prepend your test script with `nyc`. This npm script will then handle our code coverage and testing:

```json
{
"scripts": {
"test": "nyc ava"
}
}
```


## ES2015 coverage

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:

```json
{
"babel": {
"presets": ["es2015"],
"plugins": ["transform-runtime"],
"ignore": "test.js",
"env": {
"development": {
"sourceMaps": "inline"
}
}
}
}
```

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:

```json
{
"scripts": {
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
}
}
```

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:

```json
{
"scripts": {
"test": "nyc --babel --reporter=text ava"
}
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally prefer separating the coveralls stuff from the local test running. Meaning I don't see the point in having --reporter=lcov here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should look like:

"test": "nyc ava"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and in .travis.yml, you can do node_modules/.bin/nyc report --reporter=text-lcov



## HTML reports

To see a HTML report for either the ES6 or ES5 coverage strategies we have outlined, do:

```
nyc report --reporter=html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be ./node_modules/.bin/nyc unless installed globally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```

Or, convert it into an npm script for less typing:

```json
{
"scripts": {
"report": "nyc report --reporter=html"
}
}
```

This will output a HTML file to the `coverage` directory.


## Hosted coverage

### Travis CI & Coveralls

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably add that the repo needs to be activated in the Coveralls web UI.

Firstly, you will need to activate your repository in the coveralls user interface. Once that is done, add [`coveralls`] as a development dependency:

```
npm install coveralls --save-dev
```

Then add the following to your `.travis.yml`:

```
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
```

Your coverage report will then appear on coveralls shortly after the CI service completes.

[`babel`]: https://github.com/babel/babel
[`coveralls`]: https://github.com/nickmerwin/node-coveralls
[isolated-env]: https://github.com/sindresorhus/ava#isolated-environment
[`istanbul`]: https://github.com/gotwarlost/istanbul
[`nyc`]: https://github.com/bcoe/nyc
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ Concurrency is not parallelism. It enables parallelism. [Learn more.](http://sta

## Recipes

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


Expand Down