Skip to content

Commit c3bcbf2

Browse files
pearofducksnovemberborn
authored andcommitted
Improve Babel and Vue recipes
- Add webpack alias section to Babel and Vue recipes - Add exclusion of `node_modules` to `require-extension-hooks` in Vue recipe
1 parent dc552bc commit c3bcbf2

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

docs/recipes/babel.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,31 @@ Now instead of requiring `@babel/register`, require `test/_register` instead.
206206

207207
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.
208208

209+
## Webpack aliases
210+
211+
[Webpack aliases](https://webpack.js.org/configuration/resolve/#resolve-alias) can be used to provide a shortcut to deeply nested or otherwise inconvenient paths. If you already use aliases in your source files, you'll need to make sure you can use the same aliases in your test files.
212+
213+
Install `babel-plugin-webpack-alias-7` as a dev-dependency. Then add the plugin to AVA's Babel config:
214+
215+
`package.json`:
216+
217+
```json
218+
{
219+
"ava": {
220+
"babel": {
221+
"testOptions": {
222+
"plugins": [
223+
[
224+
"babel-plugin-webpack-alias-7",
225+
{
226+
"config": "./path/to/webpack.config.test.js"
227+
}
228+
]
229+
]
230+
}
231+
}
232+
}
233+
}
234+
```
209235

210236
[Babel options]: https://babeljs.io/docs/en/options

docs/recipes/vue.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/do
55
## Dependencies
66

77
- [Require extension hooks](https://github.com/jackmellis/require-extension-hooks):
8-
- `npm i --save-dev require-extension-hooks require-extension-hooks-vue require-extension-hooks-babel`
8+
- `npm i --save-dev require-extension-hooks require-extension-hooks-vue require-extension-hooks-babel@beta`
99

1010
- [browser-env](browser-testing.md)
1111
- `npm i --save-dev browser-env`
1212

13+
- Optional: [babel-plugin-webpack-alias-7](https://github.com/shortminds/babel-plugin-webpack-alias-7) if you want to use [webpack aliases](https://webpack.js.org/configuration/resolve/#resolve-alias) or use them in your source files
14+
- `npm i --save-dev babel-plugin-webpack-alias-7`
15+
1316
## Setup
1417

1518
The first step is setting up a helper to configure the environment to transpile `.vue` files and run in a browser like environment.
@@ -40,9 +43,11 @@ Vue.config.productionTip = false;
4043
// Setup vue files to be processed by `require-extension-hooks-vue`
4144
hooks('vue').plugin('vue').push();
4245
// Setup vue and js files to be processed by `require-extension-hooks-babel`
43-
hooks(['vue', 'js']).plugin('babel').push();
46+
hooks(['vue', 'js']).exclude(({filename}) => filename.match(/\/node_modules\//)).plugin('babel').push();
4447
```
4548

49+
**Note:** If you are using _babel-plugin-webpack-alias-7_, you must also exclude your webpack file - e.g. `filename.includes(/\/node_modules\//) || filename.includes('webpack.config.test.js')`
50+
4651
You can find more information about setting up Babel with AVA in the [Babel recipe](babel.md).
4752

4853
## Sample snapshot test

0 commit comments

Comments
 (0)