Skip to content

Commit 909fdac

Browse files
committed
docs: add introduction to unit tests guide
1 parent d0fd7d3 commit 909fdac

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

docs/en/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# vue-test-utils
22

3-
vue-test-utils is the official test library for Vue.js. It provides methods for unit testing Vue components.
3+
vue-test-utils is the official test library for Vue.js.
44

5-
[Documentation Summary](SUMMARY.md)
5+
It provides methods for unit testing Vue components.

docs/en/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* [trigger](api/wrapper-array/trigger.md)
3939
* [selectors](api/selectors.md)
4040
* [guides](guides/README.md)
41+
* [Introduction to unit tests](guides/introduction-to-unit-tests.md)
4142
* [Using with Jest](guides/using-with-jest.md)
4243
* [Testing SFCs with Jest](guides/testing-SFCs-with-jest.md)
4344
* [Using with Vuex](guides/using-with-vuex.md)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Introduction to unit tests
2+
3+
Unit testing is the process of writing automatic tests for units of code. In Vue, components are our units.
4+
5+
Vue Test Utils is geared towards testing Vue components. Let's look at a quick overview.
6+
7+
## Writing unit tests with Vue Test Utils
8+
9+
The process of unit testing Vue components with Vue Test Utils is to mount a component in memory. Mounting a component is a bit like setting the component running. The `mount` and `shallow` methods returns a `Wrapper` object that contains the mounted component, and helper methods to perform assertions on the component and to traverse the render tree.
10+
11+
```js
12+
import { mount } from 'vue-test-utils'
13+
14+
const wrapper = mount(Component) // returns a Wrapper containing a mounted Component instance
15+
wrapper.text() // returns text of mounted component inside Wrapper
16+
wrapper.vm // the mounted Vue instance
17+
```
18+
19+
`shallow` stubs the children of a component before it mounts it:
20+
21+
```js
22+
import { shallow } from 'vue-test-utils'
23+
24+
const wrapper = shallow(Component) // returns a Wrapper containing a mounted Component instance
25+
wrapper.vm // the mounted Vue instance
26+
```
27+
28+
Often, you'll need to pass data to the Component. For example, you might need to pass props to a component. You can do this with mount options:
29+
30+
```js
31+
import { mount } from 'vue-test-utils'
32+
33+
mount(Component, {
34+
propsData: {
35+
aProp: 'some value'
36+
}
37+
})
38+
```
39+
40+
*For a full list of options, please see the mount options section of the docs.*
41+
42+
Vue follows a pattern of injecting properties. Normally this is done in a main.js file that creates the Vue instance. This won't be run when you're unit testing, so you'll often need to add properties to the Vue instance yourself. You can do that with the `intercept` option.
43+
44+
```js
45+
import { mount } from 'vue-test-utils'
46+
47+
const $store = {
48+
getters:
49+
}
50+
mount(Component, {
51+
intercept: {
52+
$store // adds the $store object to the Vue instance before mounting component
53+
}
54+
})
55+
```
56+
57+
# Testing single file components
58+
59+
Most Vue tests will be testing single file components (SFCs). These files require precompilation before they can be run in Node or in the browser.
60+
61+
The two recommended ways are to test with a Jest preprocessor, or use Webpack.
62+
63+
You can learn how to test SFCs by reading these guides:
64+
65+
- [Testing SFCs with Jest]()
66+
67+
## Knowing what to test
68+
69+
It's impossible to say exactly what you should test and what you shouldn't, but there are some general rules you can follow.
70+
71+
You should write test for custom logic inside Vue components. The best way to test the logic of component is to make sure an input creates the expected output.
72+
73+
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.
74+
75+
## Resourcs
76+
77+
- [Getting started with Jest]()
78+
- [Testing SFCs with Jest]()
79+

0 commit comments

Comments
 (0)