Skip to content

Commit b18e37e

Browse files
committed
docs: revise
1 parent 8b98b07 commit b18e37e

11 files changed

+158
-159
lines changed

docs/en/README.md

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
# Vue Test Utils
1+
# `vue-test-utils`
22

3-
Vue Test Utils is the official test library for Vue.js.
3+
`vue-test-utils` is the official test utility library for Vue.js.
44

5-
It provides methods for [unit testing](guides/introduction-to-unit-tests.md) Vue components.
5+
It provides methods that make it easier to traverse, query and assert your Vue components' output in unit tests.
66

7-
## Example
7+
## Introduction
8+
9+
`vue-test-utils` tests Vue components by mounting them in isolation, mocking the necessary inputs (props, injections and user events) and asserting the outputs (render result, emitted custom events).
10+
11+
### Mounting Components
12+
13+
The `mount` method returns a [Wrapper](./api/wrapper.md) object that contains the mounted component, and helper methods to perform assertions on the component and to traverse the render tree.
14+
15+
```js
16+
import { mount } from 'vue-test-utils'
17+
18+
const wrapper = mount(Component) // returns a Wrapper containing a mounted Component instance
19+
wrapper.text() // returns text of mounted component inside Wrapper
20+
wrapper.vm // the mounted Vue instance
21+
```
22+
23+
If we were testing a counter component that increments its count on a button click, the test would look like this:
824

925
```js
1026
import { mount } from 'vue-test-utils'
@@ -15,17 +31,70 @@ wrapper.find('button').trigger('click')
1531
expect(wrapper.text()).toContain('1')
1632
```
1733

34+
### Stubbing Child Components with `shallow`
35+
36+
For components that contain many child components, the entire rendered tree can get really big. But in unit tests, we typically want to focus on the component being tested as a unit and avoid indirectly asserting the behavior of its child components. Also, repeatedly rendering all child components could slow down our tests. To mount a component without rendering its child components, we can use the `shallow` method which stubs the child components:
37+
38+
```js
39+
import { shallow } from 'vue-test-utils'
40+
41+
const wrapper = shallow(Component) // returns a Wrapper containing a mounted Component instance
42+
wrapper.vm // the mounted Vue instance
43+
```
44+
45+
### Mocking Props
46+
47+
You can pass props to the component using Vue's built-in `propsData` option:
48+
49+
```js
50+
import { mount } from 'vue-test-utils'
51+
52+
mount(Component, {
53+
propsData: {
54+
aProp: 'some value'
55+
}
56+
})
57+
```
58+
59+
*For a full list of options, please see the [mount options section](./api/options.md) of the docs.*
60+
61+
### Mocking Injections
62+
63+
Some Vue plugins such as Vue Router and Vuex auto-injects properties into all child components from the root instance. Since in unit tests we are mounting the component directly without a root instance, we may need to mock these injections. You can do that with the `intercept` option.
64+
65+
```js
66+
import { mount } from 'vue-test-utils'
67+
68+
const $store = {
69+
getters: {}
70+
}
71+
mount(Component, {
72+
intercept: {
73+
$store // adds the mocked $store object to the Vue instance before mounting component
74+
}
75+
})
76+
```
77+
78+
Note you can also directly inject real Vuex stores or router instances when mounting the component. See [Using with Vuex](./guides/using-with-vuex.md) and [Using with Vue Router](./guides/using-with-vue-router.md) for more details.
79+
80+
## Testing Single-File Components
81+
82+
Single-file Vue components (SFCs) require pre-compilation before they can be run in Node or in the browser. There are two recommended ways to perform the compilation: with a Jest preprocessor, or directly use webpack.
83+
84+
The `jest-vue` preprocessor supports basic SFC functionalities, but currently does not handle style blocks or custom blocks, which are only supported in `vue-loader`. If you rely on these features or other webpack-specific configurations, you will need to use a webpack + `vue-loader` based setup.
85+
86+
Read the following guides for different setups:
1887

19-
## Getting started
88+
- [Testing SFCs with Jest](./guides/testing-SFCs-with-jest.md)
89+
- [Testing SFCs with Mocha + webpack](./guides/testing-SFCs-with-mocha-webpack.md)
2090

21-
To get started using Vue Test Utils, please see [using with jest](guides/using-with-jest.md).
91+
## Knowing what to test
2292

23-
If you're new to unit testing, you can read our [introduction to unit tests](guides/introduction-to-unit-tests.md).
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.
2494

25-
## Testing single file components (SFCs)
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.
2696

27-
- [Testing SFCs with Jest](guides/testing-SFCs-with-jest.md)
28-
- [Testing SFCs with Mocha](guides/testing-SFCs-with-mocha-webpack.md)
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.
2998

3099
## Example projects
31100

docs/en/SUMMARY.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
## Table of Contents
22

3-
* [API](api/README.md)
3+
* Guides
4+
* [Choosing a test runner](guides/choosing-a-test-runner.md)
5+
* [Using with Jest](guides/using-with-jest.md)
6+
* [Testing SFCs with Jest](guides/testing-SFCs-with-jest.md)
7+
* [Testing SFCs with Mocha + webpack](guides/testing-SFCs-with-mocha-webpack.md)
8+
* [Using with Vuex](guides/using-with-vuex.md)
9+
* API
410
* [createLocalVue](api/createLocalVue.md)
511
* [mount](api/mount.md)
612
* [shallow](api/shallow.md)
7-
* [options](api/options.md)
13+
* [Mounting Options](api/options.md)
814
* [Wrapper](api/wrapper/README.md)
915
* [contains](api/wrapper/contains.md)
1016
* [find](api/wrapper/find.md)
@@ -16,7 +22,7 @@
1622
* [is](api/wrapper/is.md)
1723
* [isEmpty](api/wrapper/isEmpty.md)
1824
* [isVueInstance](api/wrapper/isVueInstance.md)
19-
* [name](api/wrapper/name.md)
25+
* [name](api/wrapper/name.md)
2026
* [update](api/wrapper/update.md)
2127
* [setData](api/wrapper/setData.md)
2228
* [setProps](api/wrapper/setProps.md)
@@ -37,11 +43,4 @@
3743
* [setProps](api/wrapper-array/setProps.md)
3844
* [trigger](api/wrapper-array/trigger.md)
3945
* [Selectors](api/selectors.md)
40-
* [Guides](guides/README.md)
41-
* [Introduction to unit tests](guides/introduction-to-unit-tests.md)
42-
* [Choosing a test runner](guides/choosing-a-test-runner.md)
43-
* [Using with Jest](guides/using-with-jest.md)
44-
* [Testing SFCs with Jest](guides/testing-SFCs-with-jest.md)
45-
* [Testing SFCs with Mocha](guides/testing-SFCs-with-mocha-webpack.md)
46-
* [Using with Vuex](guides/using-with-vuex.md)
4746
* [Common gotchas](common-gotchas.md)

docs/en/api/README.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
# API
22

3-
## [mount](api/mount.md)
4-
## [shallow](shallow.md)
5-
## [options](options.md)
6-
## [createLocalVue](createLocalVue.md)
7-
## [selectors](selectors.md)
3+
* [createLocalVue](./createLocalVue.md)
4+
* [mount](./mount.md)
5+
* [shallow](./shallow.md)
6+
* [options](./options.md)
7+
* [Wrapper](./wrapper/README.md)
8+
* [contains](./wrapper/contains.md)
9+
* [find](./wrapper/find.md)
10+
* [hasAttribute](./wrapper/hasAttribute.md)
11+
* [hasClass](./wrapper/hasClass.md)
12+
* [hasProp](./wrapper/hasProp.md)
13+
* [hasStyle](./wrapper/hasStyle.md)
14+
* [html](./wrapper/html.md)
15+
* [is](./wrapper/is.md)
16+
* [isEmpty](./wrapper/isEmpty.md)
17+
* [isVueInstance](./wrapper/isVueInstance.md)
18+
* [name](./wrapper/name.md)
19+
* [update](./wrapper/update.md)
20+
* [setData](./wrapper/setData.md)
21+
* [setProps](./wrapper/setProps.md)
22+
* [text](./wrapper/text.md)
23+
* [trigger](./wrapper/trigger.md)
24+
* [WrapperArray](./wrapper-array/README.md)
25+
* [at](./wrapper-array/at.md)
26+
* [contains](./wrapper-array/contains.md)
27+
* [hasAttribute](./wrapper-array/hasAttribute.md)
28+
* [hasClass](./wrapper-array/hasClass.md)
29+
* [hasProp](./wrapper-array/hasProp.md)
30+
* [hasStyle](./wrapper-array/hasStyle.md)
31+
* [is](./wrapper-array/is.md)
32+
* [isEmpty](./wrapper-array/isEmpty.md)
33+
* [isVueInstance](./wrapper-array/isVueInstance.md)
34+
* [update](./wrapper-array/update.md)
35+
* [setData](./wrapper-array/setData.md)
36+
* [setProps](./wrapper-array/setProps.md)
37+
* [trigger](./wrapper-array/trigger.md)
38+
* [Selectors](./selectors.md)

docs/en/api/options.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# options
1+
# Mounting Options
22

3-
Options for mount and shallow. The options object can contain vue-test-utils options and Vue options together.
3+
Options for `mount` and `shallow`. The options object can contain both `vue-test-utils` mounting options and raw Vue options.
44

5-
Vue options are passed to the component when a new instance is created. , e.g. `store`, `propsData`. For full list, see the [Vue API](https://vuejs.org/v2/api/). Also takes vue-test-utils options:
5+
Vue options are passed to the component when a new instance is created. , e.g. `store`, `propsData`. For full list, see the [Vue API](https://vuejs.org/v2/api/).
66

7-
- **Options:**
7+
## `vue-test-utils` Specific Mounting Options
88

99
- `{Object} options`
1010
- `{boolean} attachToDocument`
@@ -14,9 +14,9 @@ Vue options are passed to the component when a new instance is created. , e.g. `
1414
- `{Object} context`
1515
- `{Object} intercept`
1616
- `{Vue} localVue`
17-
- `{Object} slots`
18-
- `{Array<Componet|Object>|Component|String} default`
19-
- `{Array<Componet|Object>|Component|String} named`
17+
- `{Object} slots`
18+
- `{Array<Componet|Object>|Component|String} default`
19+
- `{Array<Componet|Object>|Component|String} named`
2020
- `{Object|Array<string>} stubs`
2121
- `{Object|Function} provide`
2222

docs/en/common-gotchas.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
## browser environment
44

5-
Vue Test Utils must be run in a browser environment. This means you can run it in a browser (not recommended), or you can run it in node using a virtual browser, like [JSDOM](https://github.com/tmpvar/jsdom).
5+
`vue-test-utils` must be run in a browser environment. This means you can run it in a browser (not recommended), or you can run it in node using a virtual browser, like [JSDOM](https://github.com/tmpvar/jsdom).
66

7-
[Jest](https://facebook.github.io/jest/docs/en/cli.html) sets up JSDOM automatically, so it is the easiest way for you to run Vue Test Utils in node.
7+
[Jest](https://facebook.github.io/jest/docs/en/cli.html) sets up JSDOM automatically, so it is the easiest way for you to run `vue-test-utils` in node.
88

99
## createLocalVue
1010

@@ -40,4 +40,4 @@ mount(Component, {
4040
}
4141
}
4242
})
43-
```
43+
```

docs/en/guides/README.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
# Guides
22

3-
## Getting started
4-
5-
- [Using with Jest](using-with-jest.md)
6-
7-
## Tesing single file components (SFCs)
8-
9-
- [Testing SFCs with Jest](testing-SFCs-with-jest.md)
10-
11-
## Using with plugins
12-
13-
- [Using with Vuex](using-with-vuex.md)
3+
* [Choosing a test runner](./choosing-a-test-runner.md)
4+
* [Using with Jest](./using-with-jest.md)
5+
* [Testing SFCs with Jest](./testing-SFCs-with-jest.md)
6+
* [Testing SFCs with Mocha + webpack](./testing-SFCs-with-mocha-webpack.md)
7+
* [Using with Vuex](./using-with-vuex.md)

docs/en/guides/choosing-a-test-runner.md

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,17 @@
22

33
A test runner is a program that runs tests.
44

5-
There are many popular JavaScript test runners, and Vue Test Utils works in all of them. It's test runner agnostic.
5+
There are many popular JavaScript test runners, and `vue-test-utils` works with all of them. It's test runner agnostic.
66

7-
However, some popular test runners do not have good support for compiling single file components (SFCs).
7+
There are a few things to consider when choosing a test runner though: feature set, performance, and support for single-file component (SFC) pre-compilation. After carefully comparing existing libraries, here are two test runners that we recommend:
88

9-
## Performance
9+
- [Jest](https://facebook.github.io/jest/docs/en/getting-started.html#content) is the most fully featured test runner. It requires the least configuration, sets up JSDOM by default, and has a great command line user experience. However, you will need a preprocessor to be able to import SFC components in your tests. We have created the `jest-vue` preprocessor which can handle most common SFC features, but it currently does not have 100% feature parity with `vue-loader`.
1010

11-
The fastest test runners are:
12-
13-
- Jest
14-
- Mocha Webpack
15-
- tape
16-
17-
You can see a performance comparisson of popular test runners [here](https://github.com/eddyerburgh/vue-unit-test-perf-comparison).
18-
19-
## Features
20-
21-
Jest is the most fully featured test runner. It requires the leat configuration, as it sets up JSDOM by default.
22-
23-
tape is very minimal. If you choose tape, you will need to do a lot of set up yourself.
24-
25-
To run SFCs in Jest, you need to use jest-vue—a transformer for Jest. jest-vue does not support styles or custom blocks. If you need to use those features, you'll need to use webpack. Mocha works well with webpack, you can read the guide on [testing SFCs with mocha](testing-SFCs-with-mocha-webpack.md) to get started.
11+
- [mocha-webpack](https://github.com/zinserjan/mocha-webpack) is a wrapper around webpack + Mocha, but with a more streamlined interface and watch mode. The benefits of this setup is that we can get complete SFC support via webpack + `vue-loader`, but it requires more configuration upfront.
2612

2713
## Getting started
2814

29-
You can use our guides to get started with a test runner:
15+
You can read the following guides to get started with either test runner:
3016

3117
- [Testing SFCs with Jest](testing-SFCs-with-jest.md)
3218
- [Testing SFCs with Mocha and webpack](testing-SFCs-with-mocha-webpack.md)
@@ -37,4 +23,4 @@ You can use our guides to get started with a test runner:
3723
- [Example project with Jest](https://github.com/eddyerburgh/vue-test-utils-jest-example)
3824
- [Example project with Mocha](https://github.com/eddyerburgh/vue-test-utils-mocha-example)
3925
- [Example project with tape](https://github.com/eddyerburgh/vue-test-utils-tape-example)
40-
- [Example project with AVA](https://github.com/eddyerburgh/vue-test-utils-ava-example)
26+
- [Example project with AVA](https://github.com/eddyerburgh/vue-test-utils-ava-example)

docs/en/guides/introduction-to-unit-tests.md

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

0 commit comments

Comments
 (0)