Skip to content

Commit b33cb1d

Browse files
authored
Babel 7 support and additional enhancements
Fixes #1598. Switches AVA's Babel implementation to use Babel 7. This applies to test and helper file compilation. Adds a `compileEnhancements` option which can be set to `false` to disable Power Assert and our `t.throws()` helper. Changes the Babel configuration. If you had this before: ```json "ava": { "babel": { "plugins": [] } } ``` You'll now need: ```json "ava": { "babel": { "testOptions": { "plugins": [] } } } ``` `ava.babel.testOptions.babelrc` now defaults to `true`. You can disable our stage-4 preset by adding `["ava/stage-4", false]` to `ava.babel.testOptions.presets`. Set `ava.babel` to `false` to disable AVA's test file compilation, **whilst still compiling enhancements**. If `compileEnhancements` is *also* set to `false` then Babel is skipped completely. Fixes #1225, #1488 and #1556.
1 parent a051d3e commit b33cb1d

40 files changed

+1199
-829
lines changed

api.js

+20-12
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ class Api extends EventEmitter {
5757
}
5858

5959
_runFile(file, runStatus, execArgv) {
60-
const hash = this.precompiler.precompileFile(file);
61-
const precompiled = Object.assign({}, this._precompiledHelpers);
62-
const resolvedfpath = fs.realpathSync(file);
63-
precompiled[resolvedfpath] = hash;
60+
const precompiled = {};
61+
if (this.precompiler) {
62+
Object.assign(precompiled, this._precompiledHelpers);
63+
const hash = this.precompiler.precompileFile(file);
64+
const resolvedfpath = fs.realpathSync(file);
65+
precompiled[resolvedfpath] = hash;
66+
}
6467

6568
const options = Object.assign({}, this.options, {precompiled});
6669
if (runStatus.updateSnapshots) {
@@ -117,19 +120,24 @@ class Api extends EventEmitter {
117120

118121
this.options.cacheDir = cacheDir;
119122

120-
const isPowerAssertEnabled = this.options.powerAssert !== false;
121-
return babelConfigHelper.build(this.options.projectDir, cacheDir, this.options.babelConfig, isPowerAssertEnabled)
123+
const compileEnhancements = this.options.compileEnhancements !== false;
124+
return babelConfigHelper.build(this.options.projectDir, cacheDir, this.options.babelConfig, compileEnhancements)
122125
.then(result => {
123-
this.precompiler = new CachingPrecompiler({
124-
path: cacheDir,
125-
getBabelOptions: result.getOptions,
126-
babelCacheKeys: result.cacheKeys
127-
});
126+
if (result) {
127+
this.precompiler = new CachingPrecompiler({
128+
path: cacheDir,
129+
getBabelOptions: result.getOptions,
130+
babelCacheKeys: result.cacheKeys
131+
});
132+
}
128133
});
129134
}
130135

131136
_precompileHelpers() {
132137
this._precompiledHelpers = {};
138+
if (!this.precompiler) {
139+
return Promise.resolve();
140+
}
133141

134142
// Assumes the tests only load helpers from within the `resolveTestsFrom`
135143
// directory. Without arguments this is the `projectDir`, else it's
@@ -138,7 +146,7 @@ class Api extends EventEmitter {
138146
// processes, avoiding the need for precompilation.
139147
return new AvaFiles({cwd: this.options.resolveTestsFrom})
140148
.findTestHelpers()
141-
.map(file => { // eslint-disable-line array-callback-return
149+
.each(file => { // eslint-disable-line array-callback-return
142150
const hash = this.precompiler.precompileFile(file);
143151
this._precompiledHelpers[file] = hash;
144152
});

docs/recipes/babel.md

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Configuring Babel
2+
3+
AVA uses [Babel 7](https://babeljs.io) so you can use the latest JavaScript syntax in your tests. We do this by compiling test and helper files using our [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) preset. We also use a [second preset](https://github.com/avajs/babel-preset-transform-test-files) to enable [enhanced assertion messages](../../readme#enhanced-assertion-messages) and detect improper use of `t.throws()` assertions.
4+
5+
By default our Babel pipeline is applied to test and helper files ending in `.js`. If your project uses Babel then we'll automatically compile files using your project's Babel configuration.
6+
7+
AVA only looks for Babel configuration files in your project directory. That is, `.babelrc` or `.babelrc.js` files next to your `package.json` file, or the `package.json` file itself.
8+
9+
## Customize how AVA compiles your test files
10+
11+
You can override the default Babel configuration AVA uses for test file compilation in `package.json`. For example, the configuration below adds support for JSX syntax and stage 3 features:
12+
13+
```json
14+
{
15+
"ava": {
16+
"babel": {
17+
"testOptions": {
18+
"plugins": ["@babel/plugin-syntax-jsx"],
19+
"presets": ["@babel/preset-stage-3"]
20+
}
21+
}
22+
}
23+
}
24+
```
25+
26+
All `.babelrc` options are allowed inside the `testOptions` object.
27+
28+
## Make AVA skip your project's Babel options
29+
30+
You may not want AVA to use your project's Babel options, for example if your project is relying on Babel 6. You can set the `babelrc` option to `false`:
31+
32+
```json
33+
{
34+
"ava": {
35+
"babel": {
36+
"testOptions": {
37+
"babelrc": false
38+
}
39+
}
40+
}
41+
}
42+
```
43+
44+
## Disable AVA's stage-4 preset
45+
46+
You can disable AVA's stage-4 preset:
47+
48+
```json
49+
{
50+
"ava": {
51+
"babel": {
52+
"testOptions": {
53+
"presets": [
54+
["module:ava/stage-4", false]
55+
]
56+
}
57+
}
58+
}
59+
}
60+
```
61+
62+
Note that this *does not* stop AVA from compiling your test files using Babel.
63+
64+
## Disable AVA's Babel pipeline
65+
66+
You can completely disable AVA's use of Babel:
67+
68+
```json
69+
{
70+
"ava": {
71+
"babel": false,
72+
"compileEnhancements": false
73+
}
74+
}
75+
```
76+
77+
## Use Babel polyfills
78+
79+
AVA lets you write your tests using new JavaScript syntax, even on Node.js versions that otherwise wouldn't support it. However, it doesn't add or modify built-ins of your current environment. Using AVA would, for example, not provide modern features such as `Object.entries()` to an underlying Node.js 6 environment.
80+
81+
By loading [Babel's `polyfill` module](https://babeljs.io/docs/usage/polyfill/) you can opt in to these features. Note that this will modify the environment, which may influence how your program behaves.
82+
83+
You can enable the `polyfill` module by adding it to AVA's `require` option:
84+
85+
```json
86+
{
87+
"ava": {
88+
"require": [
89+
"@babel/polyfill"
90+
]
91+
}
92+
}
93+
```
94+
95+
You'll need to install `@babel/polyfill` yourself.
96+
97+
## Compile sources
98+
99+
AVA does not currently compile source files. You'll have to load [Babel's `register` module](http://babeljs.io/docs/usage/require/), which will compile source files as needed.
100+
101+
You can enable the `register` module by adding it to AVA's `require` option:
102+
103+
```json
104+
{
105+
"ava": {
106+
"require": [
107+
"@babel/register"
108+
]
109+
}
110+
}
111+
```
112+
113+
You'll need to install `@babel/register` yourself.
114+
115+
Note that loading `@babel/register` in every worker process has a non-trivial performance cost. If you have lots of test files, you may want to consider using a build step to compile your sources *before* running your tests. This isn't ideal, since it complicates using AVA's watch mode, so we recommend using `@babel/register` until the performance penalty becomes too great. Setting up a precompilation step is out of scope for this document, but we recommend you check out one of the many [build systems that support Babel](http://babeljs.io/docs/setup/). There is an [issue](https://github.com/avajs/ava/issues/577) discussing ways we could make this experience better.

docs/recipes/babelrc.md

+3-145
Original file line numberDiff line numberDiff line change
@@ -2,150 +2,8 @@
22

33
Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/babelrc.md)
44

5-
There are multiple options for configuring how AVA transpiles your tests using Babel.
5+
This recipe has moved. Please see our [new Babel recipe](babel.md).
66

7-
- [AVA's default transpiler behavior](#avas-default-transpiler-behavior)
8-
- [Customizing how AVA transpiles your tests](#customizing-how-ava-transpiles-your-tests)
9-
- [Transpiling Sources](#transpiling-sources)
10-
- [Transpiling tests and sources the same way](#transpiling-tests-and-sources-the-same-way)
11-
- [Extend your source transpilation configuration](#extend-your-source-transpilation-configuration)
12-
- [Extend an alternate config file (i.e. not `.babelrc`)](#extend-an-alternate-config-file)
7+
---
138

14-
## AVA's default transpiler behavior
15-
16-
AVA lets you use some nifty JavaScript features, like [async functions](https://github.com/avajs/ava#async-function-support). To make this work on older Node.js versions AVA transpiles the tests and helper files using the [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) Babel preset. This is great for projects where you do not use Babel for your source, but do want to use the newest JavaScript features for your tests.
17-
18-
### Using object rest/spread properties
19-
20-
As of Node.js [8.3.0](https://github.com/nodejs/node/blob/v8.3.0/doc/changelogs/CHANGELOG_V8.md#8.3.0) [object rest/spread properties](https://github.com/tc39/proposal-object-rest-spread) is supported directly. This new language feature however has not yet reached [stage 4](http://2ality.com/2015/11/tc39-process.html#stage-4-finished), which means that AVA won't support it by default. That said, if you're using Node.js 8.3.0 or newer, AVA will load the [`syntax-object-rest-spread`](https://www.npmjs.com/package/babel-plugin-syntax-object-rest-spread) plugin for you. This means you *can* use object rest/spread properties in your tests as long as you're not targeting older Node.js versions.
21-
22-
Note that if you customize the Babel configuration you'll have to explicitly specify the `syntax-object-rest-spread` plugin.
23-
24-
## Customizing how AVA transpiles your tests
25-
26-
You can override the default Babel configuration AVA uses for test transpilation in `package.json`. For example, the configuration below adds the Babel `rewire` plugin, and adds the Babel [`stage-3`](http://babeljs.io/docs/plugins/preset-stage-3/) preset.
27-
28-
```json
29-
{
30-
"ava": {
31-
"babel": {
32-
"plugins": ["rewire"],
33-
"presets": ["@ava/stage-4", "stage-3"]
34-
}
35-
}
36-
}
37-
```
38-
39-
Note that this only affects how AVA transpiles your tests. If you use `babel-register` you'll still need to add separate Babel configuration as explained [here](#transpiling-sources).
40-
41-
## Use Babel Polyfills
42-
43-
AVA lets you write your tests using new JavaScript syntax, even on Node.js versions that otherwise wouldn't support it. However, it doesn't add or modify built-ins of your current environment. Using AVA would, for example, not provide modern features such as `Array.prototype.includes()` to an underlying Node.js 4 environment.
44-
45-
By loading [Babel's Polyfill module](https://babeljs.io/docs/usage/polyfill/) you can opt in to these features. Note that this will modify the environment, which may influence how your program behaves.
46-
47-
You can enable `babel-polyfill` by adding it to AVA's `require` option:
48-
49-
```json
50-
{
51-
"ava": {
52-
"require": [
53-
"babel-polyfill"
54-
]
55-
}
56-
}
57-
```
58-
59-
## Transpiling Sources
60-
61-
To transpile your sources, you will need to define a [`babel config` ](http://babeljs.io/docs/usage/babelrc/) in `package.json` or a `.babelrc` file. Also, you will need to tell AVA to load [`babel-register`](http://babeljs.io/docs/usage/require/) in every forked process, by adding it to the `require` section of your AVA config:
62-
63-
`package.json`
64-
65-
```json
66-
{
67-
"ava": {
68-
"require": ["babel-register"]
69-
},
70-
"babel": {
71-
"presets": ["@ava/stage-4"]
72-
}
73-
}
74-
```
75-
76-
Note that loading `babel-register` in every forked process has a non-trivial performance cost. If you have lots of test files, you may want to consider using a build step to transpile your sources *before* running your tests. This isn't ideal, since it complicates using AVA's watch mode, so we recommend using `babel-register` until the performance penalty becomes too great. Setting up a precompilation step is out of scope for this document, but we recommend you check out one of the many [build systems that support Babel](http://babeljs.io/docs/setup/). There is an [open issue](https://github.com/avajs/ava/issues/577) discussing ways we could make this experience better.
77-
78-
## Transpiling tests and sources the same way
79-
80-
Using the `"inherit"` shortcut will cause your tests to be transpiled the same as your sources (as specified in your [`babelrc`](http://babeljs.io/docs/usage/babelrc/)). AVA will add a few additional [internal plugins](#notes) when transpiling your tests, but they won't affect the behavior of your test code.
81-
82-
`package.json`:
83-
84-
```json
85-
{
86-
"ava": {
87-
"require": "babel-register",
88-
"babel": "inherit"
89-
},
90-
"babel": {
91-
"presets": [
92-
"@ava/stage-4",
93-
"react"
94-
]
95-
}
96-
}
97-
```
98-
99-
In the above example, both tests and sources will be transpiled using the [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) and [`react`](http://babeljs.io/docs/plugins/preset-react/) presets.
100-
101-
AVA will only look for a `.babelrc` file in the same directory as the `package.json` file. If not found then it assumes your Babel config lives in the `package.json` file.
102-
103-
## Extend your source transpilation configuration
104-
105-
When specifying the Babel config for your tests, you can set the `babelrc` option to `true`. This will merge the specified plugins with those from your [`babelrc`](http://babeljs.io/docs/usage/babelrc/).
106-
107-
`package.json`:
108-
109-
```json
110-
{
111-
"ava": {
112-
"require": "babel-register",
113-
"babel": {
114-
"babelrc": true,
115-
"plugins": ["custom-plugin-name"],
116-
"presets": ["custom-preset"]
117-
}
118-
},
119-
"babel": {
120-
"presets": [
121-
"@ava/stage-4",
122-
"react"
123-
]
124-
}
125-
}
126-
```
127-
128-
In the above example, *sources* are compiled use [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) and [`react`](http://babeljs.io/docs/plugins/preset-react/), *tests* use those same plugins, plus the additional `custom` plugins specified.
129-
130-
AVA will only look for a `.babelrc` file in the same directory as the `package.json` file. If not found then it assumes your Babel config lives in the `package.json` file.
131-
132-
## Extend an alternate config file.
133-
134-
If, for some reason, your Babel config is not specified in one of the default locations ([`.babelrc` or `package.json`](http://babeljs.io/docs/usage/babelrc/), you can set the `extends` option to the alternate config you want to use during testing.
135-
136-
`package.json`:
137-
138-
```json
139-
{
140-
"ava": {
141-
"require": "babel-register",
142-
"babel": {
143-
"extends": "./babel-test-config.json",
144-
"plugins": ["custom-plugin-name"],
145-
"presets": ["custom-preset"]
146-
}
147-
}
148-
}
149-
```
150-
151-
The above uses `babel-test-config.json` as the transpilation config for *sources*, and as the base config for *tests*. For *tests*, it extends that base config with the custom plugins and presets specified.
9+
If you're using an older AVA version, please see [version 0.25.0 of this recipe](https://github.com/avajs/ava/blob/v0.25.0/docs/recipes/babelrc.md).

docs/recipes/code-coverage.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
> **Please note, this recipe has not yet been updated for Babel 7 support in AVA 1.0.**
2+
3+
---
4+
15
# Code coverage
26

37
Translations: [Español](https://github.com/avajs/ava-docs/blob/master/es_ES/docs/recipes/code-coverage.md), [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/code-coverage.md), [Italiano](https://github.com/avajs/ava-docs/blob/master/it_IT/docs/recipes/code-coverage.md), [日本語](https://github.com/avajs/ava-docs/blob/master/ja_JP/docs/recipes/code-coverage.md), [Português](https://github.com/avajs/ava-docs/blob/master/pt_BR/docs/recipes/code-coverage.md), [Русский](https://github.com/avajs/ava-docs/blob/master/ru_RU/docs/recipes/code-coverage.md), [简体中文](https://github.com/avajs/ava-docs/blob/master/zh_CN/docs/recipes/code-coverage.md)

docs/recipes/jspm-systemjs.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
> **Please note, this recipe has not yet been updated for Babel 7 support in AVA 1.0.**
2+
3+
---
4+
15
# JSPM and SystemJS for ES2015
26

37
Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/jspm-systemjs.md)

docs/recipes/precompiling-with-webpack.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
> **Please note, this recipe has not yet been updated for Babel 7 support in AVA 1.0.**
2+
3+
---
4+
15
## Precompiling source files with webpack
26

37
Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/precompiling-with-webpack.md)

docs/recipes/react.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
> **Please note, this recipe has not yet been updated for Babel 7 support in AVA 1.0.**
2+
3+
---
4+
15
# Testing React components
26

37
Translations: [Español](https://github.com/avajs/ava-docs/blob/master/es_ES/docs/recipes/react.md), [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/react.md)

docs/recipes/vue.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
> **Please note, this recipe has not yet been updated for Babel 7 support in AVA 1.0.**
2+
3+
---
4+
15
# Testing Vue.js components
26

37
Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/vue.md)

0 commit comments

Comments
 (0)