Translations: Español, Français, Italiano, 日本語, Português, Русский, 简体中文
As AVA spawns the test files, you can't use istanbul for code coverage; instead, you can achieve this with nyc which is basically istanbul with sub-process support.
First install NYC:
$ npm install nyc --save-dev
Then add both the .nyc_output and coverage directories to your .gitignore file.
.gitignore:
node_modules
coverage
.nyc_output
Using NYC to provide coverage for production code written in ES5 is simple. Just prepend your test script with nyc:
{
"scripts": {
"test": "nyc ava"
}
}That's it!
If you want to create HTML coverage reports, or upload coverage data to Coveralls, you should skip down to those sections below.
Using Babel to transpile your production code is a bit more involved. Here we've broken it down into multiple steps.
First, we need a Babel configuration. The following is just an example. You will need to modify it to fit your needs.
package.json:
{
"babel": {
"presets": ["es2015"],
"plugins": ["transform-runtime"],
"ignore": "test.js",
"env": {
"development": {
"sourceMaps": "inline"
}
}
}
}There are two important things to note from the example above.
-
We ignore test files because AVA already handles transpiling tests for you.
-
We specify
inlinesource maps for development. This is important for properly generating coverage. Using theenvsection of the Babel configuration allows us to disable source maps for production builds.
Since it is unlikely you want inline source maps in your production code. You should specify an alternate environment variable in your build scripts:
package.json
{
"scripts": {
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
}
}WARNING:
BABEL_ENV=productiondoes not work on Windows, you must use thesetkeyword (set BABEL_ENV=production). For cross platform builds, check outcross-env.
Note that the build script really has very little to do with AVA, and is just a demonstration of how to use Babel's env configuration to manipulate your config so it's compatible with AVA.
To use the Babel require hook, add babel-core/register to the require section of you AVA config in package.json.
{
"ava": {
"require": ["babel-core/register"]
}
}Combining the above steps, your complete package.json should look something like this:
{
"scripts": {
"test": "nyc ava",
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
},
"babel": {
"presets": ["es2015"],
"plugins": ["transform-runtime"],
"ignore": "test.js",
"env": {
"development": {
"sourceMaps": "inline"
}
}
},
"ava": {
"require": ["babel-core/register"]
}
}NYC creates a json coverage file for each forked process in the .nyc_ouput directory.
To combine those into a human readable HTML report, do the following:
$ ./node_modules/.bin/nyc report --reporter=html
Or, use an npm script to save on typing:
{
"scripts": {
"report": "nyc report --reporter=html"
}
}This will output a HTML file to the coverage directory.
First, you must login to coveralls.io and activate your repository.
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 Travis completes.