Skip to content

Commit ff3bba9

Browse files
blake-newmansindresorhus
authored andcommitted
Add Vue.js recipe (#1361)
1 parent 0b47071 commit ff3bba9

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

docs/recipes/vue.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Testing Vue.js components
2+
3+
## Dependencies
4+
5+
- [Require extension hooks](https://github.com/jackmellis/require-extension-hooks):
6+
- `npm i --save-dev require-extension-hooks require-extension-hooks-vue require-extension-hooks-babel`
7+
8+
- [browser-env](browser-testing.md)
9+
- `npm i --save-dev browser-env`
10+
11+
## Setup
12+
13+
The first step is setting up a helper to configure the environment to transpile `.vue` files and run in a browser like environment:
14+
15+
```json
16+
{
17+
"ava": {
18+
"babel": "inherit",
19+
"require": [
20+
"./test/helpers/setup.js"
21+
]
22+
}
23+
}
24+
```
25+
26+
```js
27+
// ./test/helpers/setup.js
28+
29+
// Setup browser environment
30+
require('browser-env')();
31+
const hooks = require('require-extension-hooks');
32+
const Vue = require('vue');
33+
34+
// Setup Vue.js to remove production tip
35+
Vue.config.productionTip = false;
36+
37+
// Setup vue files to be processed by `require-extension-hooks-vue`
38+
hooks('vue').plugin('vue').push();
39+
// Setup vue and js files to be processed by `require-extension-hooks-babel`
40+
hooks(['vue', 'js']).plugin('babel').push();
41+
```
42+
43+
You can find more information about setting up Babel with AVA in the [babelrc recipe](babelrc.md).
44+
45+
## Sample snapshot test
46+
47+
```js
48+
import test from 'ava';
49+
import Vue from 'vue';
50+
import Component from 'component.vue';
51+
52+
test('renders', t => {
53+
const vm = new Vue(Component).$mount();
54+
const tree = {
55+
$el: vm.$el.outerHTML
56+
};
57+
t.snapshot(tree);
58+
});
59+
```
60+
61+
## Coverage reporting
62+
63+
Follow the [coverage reporting recipe](code-coverage.md), additionally adding the `.vue` extension to the `nyc` config to instrument `.vue` files.
64+
65+
```json
66+
{
67+
"nyc": {
68+
"extension": [
69+
".js",
70+
".vue"
71+
]
72+
}
73+
}
74+
```

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ It's the [Andromeda galaxy](https://simple.wikipedia.org/wiki/Andromeda_galaxy).
11591159
- [TypeScript](docs/recipes/typescript.md)
11601160
- [Configuring Babel](docs/recipes/babelrc.md)
11611161
- [Testing React components](docs/recipes/react.md)
1162+
- [Testing Vue.js components](docs/recipes/vue.md)
11621163
- [JSPM and SystemJS](docs/recipes/jspm-systemjs.md)
11631164
- [Debugging tests with Chrome DevTools](docs/recipes/debugging-with-chrome-devtools.md)
11641165
- [Debugging tests with WebStorm](docs/recipes/debugging-with-webstorm.md)

0 commit comments

Comments
 (0)