|
2 | 2 |
|
3 | 3 | Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/babelrc.md)
|
4 | 4 |
|
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). |
6 | 6 |
|
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 | +--- |
13 | 8 |
|
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). |
0 commit comments