You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
nyc is now officially part of Istanbul. They also have a tutorial on how
to integrate it with Babel. We should simplify the recipe so it's only
about using nyc with AVA. I've left the .gitignore bit, but removed
Coveralls since it's not directly related to AVA.
As AVA [spawns the test files][process-isolation], you can't use [`istanbul`] for code coverage; instead, you can achieve this with [`nyc`] which is basically [`istanbul`] with sub-process support.
10
-
11
-
## Setup
5
+
Use the [nyc] command-line-client for [Istanbul] to compute the code coverage of your tests.
12
6
13
-
First install NYC:
7
+
First install [nyc]:
14
8
15
9
```
16
-
$ npm install nyc --save-dev
10
+
$ npm install --save-dev nyc
17
11
```
18
12
19
-
Then add both the `.nyc_output` and `coverage` directories to your `.gitignore` file.
20
-
21
-
`.gitignore`:
22
-
23
-
```
24
-
node_modules
25
-
coverage
26
-
.nyc_output
27
-
```
28
-
29
-
## ES5 coverage
30
-
31
-
Using NYC to provide coverage for production code written in ES5 is simple. Just prepend your test script with `nyc`:
13
+
At its simplest run AVA through [nyc]. In your `package.json` file:
32
14
33
15
```json
34
16
{
@@ -38,146 +20,14 @@ Using NYC to provide coverage for production code written in ES5 is simple. Just
38
20
}
39
21
```
40
22
41
-
That's it!
23
+
You may want to exclude the `.nyc_output` and `coverage` directories from source control. Assuming you're using Git, add the following to your `.gitignore` file:
42
24
43
-
If you want to create HTML coverage reports, or upload coverage data to Coveralls, you should skip down to those sections below.
44
-
45
-
## ES2015 coverage
46
-
47
-
Using Babel to transpile your production code is a bit more involved. Here we've broken it down into multiple steps.
48
-
49
-
### Configure Babel
50
-
51
-
First, we need a Babel configuration. The following is just an example. You will need to modify it to fit your needs.
52
-
53
-
`package.json`:
54
-
```json
55
-
{
56
-
"babel": {
57
-
"presets": ["es2015"],
58
-
"plugins": ["transform-runtime"],
59
-
"ignore": "test.js",
60
-
"env": {
61
-
"development": {
62
-
"sourceMaps": "inline"
63
-
}
64
-
}
65
-
}
66
-
}
67
25
```
68
-
69
-
There are two important things to note from the example above.
70
-
71
-
1. We ignore test files because AVA already handles transpiling tests for you.
72
-
73
-
2. We specify `inline` source maps for development. This is important for properly generating coverage. Using the `env` section of the Babel configuration allows us to disable source maps for production builds.
74
-
75
-
76
-
### Create a build script
77
-
78
-
Since it is unlikely you want `inline` source maps in your production code. You should specify an alternate environment variable in your build scripts:
> WARNING: `BABEL_ENV=production` does not work on Windows, you must use the `set` keyword (`set BABEL_ENV=production`). For cross platform builds, check out [`cross-env`].
91
-
92
-
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.
93
-
94
-
### Use the Babel require hook
95
-
96
-
To use the Babel require hook, add `babel-core/register` to the `require` section of you AVA config in `package.json`.
97
-
98
-
```json
99
-
{
100
-
"ava": {
101
-
"require": ["babel-core/register"]
102
-
}
103
-
}
104
-
```
105
-
106
-
### Putting it all together
107
-
108
-
Combining the above steps, your complete `package.json` should look something like this:
Your coverage report will then appear on coveralls shortly after Travis completes.
30
+
If you're compiling your source files using Babel you may want to apply Istanbul's instrumentation as part of the source file compilation. This will yield better results than instrumenting Babel's output. See Istanbul's [*Using Istanbul With ES2015+* tutorial](https://istanbul.js.org/docs/tutorials/es2015/). AVA sets `NODE_ENV=test` for you. Note that as of February 2018 this tutorial hasn't yet been updated for Babel 7.
0 commit comments