|
| 1 | +# Configuring Babel |
| 2 | + |
| 3 | +There are multiple options for configuring how AVA transpiles your tests using Babel. |
| 4 | + |
| 5 | + - [Specify a complete config in `package.json`](#specify-a-complete-config-in-packagejson) |
| 6 | + - [Extend existing `.babelrc` without modification](#extend-existing-babelrc-without-modification) |
| 7 | + - [Extend existing `.babelrc` with additional plugins or presets](#extend-existing-babelrc-with-additional-plugins-or-presets) |
| 8 | + - [Extend an alternate config file (i.e. not `.babelrc`)](#extend-alternate-config-file) |
| 9 | + - [Notes](#notes) |
| 10 | + |
| 11 | +## Specify a complete config in `package.json` |
| 12 | + |
| 13 | +The `babelrc` option defaults to `false`, meaning `.babelrc` files are not considered when transpiling tests. This means you must specify your complete config in `package.json`. |
| 14 | + |
| 15 | +```json |
| 16 | +{ |
| 17 | + "ava": { |
| 18 | + "babel": { |
| 19 | + "plugins": ["rewire"], |
| 20 | + "presets": ["es2015"] |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +## Extend existing `.babelrc` without modification |
| 27 | + |
| 28 | +Use the `"inherit"` shortcut if you want your tests transpiled the same as your sources. This will use your `.babelrc` directly (with a few additional [internal plugins](#notes)). |
| 29 | + |
| 30 | +`package.json`: |
| 31 | + |
| 32 | +```json |
| 33 | +{ |
| 34 | + "ava": { |
| 35 | + "babel": "inherit" |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## Extend existing `.babelrc` with additional plugins or presets |
| 41 | + |
| 42 | +Set `babelrc` to `true`. This will use your `.babelrc` and extend it with any additional plugins specified. |
| 43 | + |
| 44 | +`package.json`: |
| 45 | + |
| 46 | +```json |
| 47 | +{ |
| 48 | + "ava": { |
| 49 | + "babel": { |
| 50 | + "babelrc": true, |
| 51 | + "plugins": ["custom-plugin-name"], |
| 52 | + "presets": ["custom-preset"] |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Extend alternate config file. |
| 59 | + |
| 60 | + |
| 61 | +If, for some reason, you do not want to extend `.babelrc`, set the `extends` option to the alternate config you want to use during testing. |
| 62 | + |
| 63 | +`package.json`: |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + "ava": { |
| 68 | + "babel": { |
| 69 | + "extends": "./babel-test-config.json", |
| 70 | + "plugins": ["custom-plugin-name"], |
| 71 | + "presets": ["custom-preset"] |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +The above uses `babel-test-config.json` as the base config, and extends it with the custom plugins and presets specified. |
| 78 | + |
| 79 | +## Notes |
| 80 | + |
| 81 | +AVA *always* adds a few custom Babel plugins when transpiling your plugins. They serve a variety of functions: |
| 82 | + |
| 83 | + * Enable `power-assert` support. |
| 84 | + * Rewrite require paths internal AVA dependencies like `babel-runtime` (important if you are still using `npm@2`). |
| 85 | + * Generate test metadata to determine which files should be run first (*future*). |
| 86 | + * Static analysis of dependencies for precompilation (*future*). |
0 commit comments