Skip to content

Commit 32857f2

Browse files
committed
docs: further brush up
1 parent 1b701b9 commit 32857f2

File tree

10 files changed

+109
-135
lines changed

10 files changed

+109
-135
lines changed

docs/LANGS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* [0.0.1 - English](en/)
1+
* [English](en/)

docs/en/README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,14 @@ Read the following guides for different setups:
8888
- [Testing SFCs with Jest](./guides/testing-SFCs-with-jest.md)
8989
- [Testing SFCs with Mocha + webpack](./guides/testing-SFCs-with-mocha-webpack.md)
9090

91-
## Knowing what to test
91+
### Knowing What to Test
9292

93-
It's difficult to generalize what you should test and what you shouldn't, because it's a trade-off that has to do with your development requirements and time constraints. However, there are some general rules you can follow.
93+
For UI components, we don't recommend aiming for complete line-based coverage, because it leads to too much focus on the internal implementation details of the components and could result in brittle tests.
9494

95-
You should write tests to assert your component's logic, not implementation details. The best way to test the logic of component is to make sure an input (user interaction or change of props) creates the expected output.
95+
Instead, we recommend writing tests that assert your component's public interface, and treat its internals as a black box. A single test case would assert that some input (user interaction or change of props) provided to the component results in the expected output (render result or emitted custom events).
9696

97-
For example, imagine we have a `Counter` component that increments a display counter by 1 each time a button is clicked. The test should perform the click and assert that the counter increased by 1. This ensures that your test is not implementation specific. You can rewrite the logic of the test, and as long as clicking a button still updates the counter text, the test will pass.
97+
For example, imagine we have a `Counter` component that increments a display counter by 1 each time a button is clicked. Its test case would simulate the click and assert that the rendered output has increased by 1. The test doesn't care about how the Counter increments the value, it only cares about the input and the output.
9898

99-
## Example projects
99+
The benefit of this approach is that as long as your component's public interface remains the same, your tests will pass no matter how the component's internal implementation changes over time.
100100

101-
- [example with Jest](https://github.com/eddyerburgh/vue-test-utils-jest-example)
102-
- [example with Mocha](https://github.com/eddyerburgh/vue-test-utils-mocha-example)
103-
- [example with tape](https://github.com/eddyerburgh/vue-test-utils-tape-example)
104-
- [example with AVA](https://github.com/eddyerburgh/vue-test-utils-ava-example)
101+
This topic is discussed with more details in a [great presentation by Matt O'Connell](http://slides.com/mattoconnell/deck#/).

docs/en/SUMMARY.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
## Table of Contents
22

3-
* Guides
3+
* [Guides](guides/README.md)
44
* [Choosing a test runner](guides/choosing-a-test-runner.md)
55
* [Using with Jest](guides/using-with-jest.md)
66
* [Testing SFCs with Jest](guides/testing-SFCs-with-jest.md)
77
* [Testing SFCs with Mocha + webpack](guides/testing-SFCs-with-mocha-webpack.md)
88
* [Using with Vuex](guides/using-with-vuex.md)
9-
* API
9+
* [General Tips](guides/general-tips.md)
10+
* [API](api/README.md)
1011
* [createLocalVue](api/createLocalVue.md)
1112
* [mount](api/mount.md)
1213
* [shallow](api/shallow.md)
@@ -43,4 +44,3 @@
4344
* [setProps](api/wrapper-array/setProps.md)
4445
* [trigger](api/wrapper-array/trigger.md)
4546
* [Selectors](api/selectors.md)
46-
* [Common gotchas](common-gotchas.md)

docs/en/api/createLocalVue.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
- **Usage:**
77

8-
createLocalVue returns a Vue class for you to add components, mixins and install plugins without polluting the global Vue class.
8+
`createLocalVue` returns a Vue class for you to add components, mixins and install plugins without polluting the global Vue class.
99

1010
Use it with `options.localVue`
1111

@@ -25,4 +25,4 @@ const freshWrapper = shallow(Foo)
2525
expect(freshWrapper.vm.foo).to.equal(false)
2626
```
2727

28-
- **See also:** [Common Gotchas](common-gotchas.md)
28+
- **See also:** [General Tips](../guides/general-tips.md)

docs/en/common-gotchas.md

Lines changed: 0 additions & 43 deletions
This file was deleted.

docs/en/guides/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
* [Testing SFCs with Jest](./testing-SFCs-with-jest.md)
66
* [Testing SFCs with Mocha + webpack](./testing-SFCs-with-mocha-webpack.md)
77
* [Using with Vuex](./using-with-vuex.md)
8+
* [General Tips](./general-tips.md)

docs/en/guides/general-tips.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# General Tips
2+
3+
## Browser Environment
4+
5+
`vue-test-utils` relies on a browser environment. Technically you can run it in a real browser, but it's not recommended due to the complexity of launching real browsers on different platforms. Instead, we recommend running the tests in Node.js with a virtual browser environment using [JSDOM](https://github.com/tmpvar/jsdom).
6+
7+
The Jest test runner sets up JSDOM automatically. For other test runners, you can manually set up JSDOM for the tests using [jsdom-global](https://github.com/rstacruz/jsdom-global) in the entry for your tests:
8+
9+
``` bash
10+
npm install --save-dev jsdom jsdom-global
11+
```
12+
---
13+
``` js
14+
// in test setup / entry
15+
require('jsdom-global')()
16+
```
17+
18+
## Testing Components that Rely on Global Plugins and Mixins
19+
20+
Some of the components may rely on features injected by a global plugin or mixin, for example `vuex` and `vue-router`.
21+
22+
If you are writing tests for components in a specific app, you can setup the same global plugins and mixins once in the entry of your tests. But in some cases, for example testing a generic component suite that may get shared across different apps, it's better to test your components in a more isolated setup, without polluting the global `Vue` constructor. We can use the [createLocalVue](../api/createLocalVue.md) method to achieve that:
23+
24+
``` js
25+
import createLocalVue from 'vue-test-utils'
26+
27+
// create an extended Vue constructor
28+
const localVue = createLocalVue()
29+
30+
// install plugins as normal
31+
localVue.use(MyPlugin)
32+
33+
// pass the localVue to the mount options
34+
mount(Component, {
35+
localVue
36+
})
37+
```

docs/en/guides/testing-SFCs-with-jest.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ We need to add a `.babelrc` file, to tell babel to use the env preset:
4242
}
4343
```
4444

45-
By default, Jest doesn't recognize .vue files. So we need to tell Jest how to handle them. To do that we need to install jest-vue, which preprocesses .vue files:
45+
By default, Jest doesn't recognize `.vue` files. So we need to tell Jest how to handle them. To do that we need to install jest-vue, which preprocesses `.vue` files:
4646

4747
```
4848
npm install --sav-dev jest-vue
4949
```
5050

5151
In our `package.json`, we need to add a field to tell Jest how to treat .vue files:
52+
5253
```json
5354
// package.json
5455
{

docs/en/guides/testing-SFCs-with-mocha-webpack.md

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,35 @@ Next we need to define a unit script in our `package.json`.
2323
}
2424
```
2525

26-
This script tells mocha-webpack to get the webpack config from build/webpack.conf.js, run all test files in the test directory and run test/setup.js before the tests.
26+
This script tells `mocha-webpack` to get the webpack config from `build/webpack.conf.js`, run all test files in the test directory and run `test/setup.js` before the tests.
27+
28+
### Setting Up Browser Environment
2729

2830
Let's create the setup.js script first.
2931

30-
`vue-test-utils` requires a browser environment to run. We can set one up using browser-env (a wrapper around JSDOM).
32+
`vue-test-utils` requires a browser environment to run. We can set one up using `jsdom-global`, which setups a JSDOM instance and attaches necessary globals to the Node process.
3133

32-
Let's install that:
34+
Let's install the dependencies:
3335

34-
```
35-
npm install --save-dev browser-env
36+
```bash
37+
npm install --save-dev jsdom jsdom-global
3638
```
3739

38-
Create a test/setup.js file and paste the following code in:
40+
Create a `test/setup.js` file and paste the following code in:
3941

40-
```
41-
require('browser-env')();
42+
``` js
43+
require('jsdom-global')()
4244
```
4345

4446
This adds a browser environment to node, so that `vue-test-utils` can run correctly.
4547

46-
Next, we need to install babel and configure it so we can use ES6 features in our JavaScript:
48+
### Configuring webpack
4749

48-
```bash
49-
npm install --save-dev babel-core babel-preset-env
50-
```
50+
Now we need to create a webpack config file. In most cases your test config should use the same `module` rules with your projects existing webpack config. We recommend extracting the common config options into a base file and extend it separately for build and testing.
5151

52-
and create a .babelrc file in the root of the project, that tells babel to use the env preset:
52+
One specific tweak needed for the test config is that we should externalize Node dependencies with `webpack-node-externals`. This significantly speeds up the bundling process.
5353

54-
```json
55-
// .babelrc
56-
{
57-
"presets": ["env"]
58-
}
59-
```
60-
61-
*babel-preset-env allows compiling the JS based on the browsers you plan to support. Get more info here: [babel-preset-env](https://github.com/babel/babel-preset-env)*
62-
63-
Now we need to create a webpack config file. Create a build/webpack.conf.js file, and add the following code:
54+
For our example project, the config looks like this:
6455

6556
```js
6657
const nodeExternals = require('webpack-node-externals')
@@ -84,17 +75,17 @@ module.exports = {
8475
}
8576
```
8677

87-
That's the setup done. Now to add a test.
88-
78+
### Configuring Babel
8979

90-
## Adding a test
80+
Notice that we are using `babel-loader` to handle JavaScript. You should already have Babel configured if you are using it in your app, e.g. via a `.babelrc` file. Here `babel-loader` will automatically use the same config file.
9181

92-
Create a file in src named Counter.vue.
82+
One thing to note is that if you are using Node 6+, which already supports the majority of ES2015 features, you can configure a separate Babel [env option](https://babeljs.io/docs/usage/babelrc/#env-option) that only transpiles features that are not already supported in the Node version you are using (e.g. `stage-2` or flow syntax support, etc.)
9383

94-
Paste the following code in src/Counter.vue:
84+
### Adding a test
9585

86+
Create a file in `src` named `Counter.vue`:
9687

97-
```vue
88+
``` html
9889
<template>
9990
<div>
10091
{{ count }}
@@ -119,7 +110,7 @@ export default {
119110
</script>
120111
```
121112

122-
And create a test file`test/Counter.spec.js`. Paste the code below into the file:
113+
And create a test file named `test/Counter.spec.js` with the following code:
123114

124115
```js
125116
import { shallow } from 'vue-test-utils'
@@ -135,7 +126,7 @@ describe('Counter.vue', () => {
135126
})
136127
```
137128

138-
Notice we're using expect from chai to make our assertion. We need to install chai before running the tests:
129+
Notice we're using `expect` from `chai` to make our assertion. We need to install chai before running the tests:
139130

140131
```
141132
npm install --save-dev chai
@@ -147,7 +138,7 @@ And now we can run the test:
147138
npm run unit
148139
```
149140

150-
Great, the tests are running!
141+
Woohoo, we got our tests running!
151142

152143
### Resources
153144

0 commit comments

Comments
 (0)