You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/v2/cookbook/unit-testing-vue-components.md
+19-14Lines changed: 19 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ order: 6
6
6
7
7
## Simple Example
8
8
9
-
Unit testing is a fundamental part of software development. Unit tests execute the smallest units of code in isolation, in order to increase ease of adding new features and track down bugs. Vue's [single-file components](./single-file-components.html) make it straight forward to write unit tests for components in isolation. This lets you develop new features with confidence you are not breaking existing ones, and helps other developers understand what your component does.
9
+
Unit testing is a fundamental part of software development. Unit tests execute the smallest units of code in isolation, in order to increase ease of adding new features and track down bugs. Vue's [single-file components](../guide/single-file-components.html) make it straight forward to write unit tests for components in isolation. This lets you develop new features with confidence you are not breaking existing ones, and helps other developers understand what your component does.
10
10
11
11
This simple example tests whether some text is rendered:
12
12
@@ -44,13 +44,13 @@ export default {
44
44
```
45
45
46
46
```js
47
-
import { shallow } from'vue-test-utils'
47
+
import { shallow } from'@vue/test-utils'
48
48
49
49
test('Foo', () => {
50
50
// render the component
51
51
constwrapper=shallow(Hello)
52
52
53
-
// should not allow for username less than 7 characters, excludes whitespace
53
+
// should not allow for `username` less than 7 characters, excludes whitespace
54
54
wrapper.setData({ username:''.repeat(7) })
55
55
56
56
// assert the error is rendered
@@ -71,6 +71,7 @@ The above code snippet shows how to test whether an error message is rendered ba
71
71
## Why test?
72
72
73
73
Component unit tests have lots of benefits:
74
+
74
75
- Provide documentation on how the component should behave
75
76
- Save time over testing manually
76
77
- Reduce bugs in new features
@@ -81,11 +82,12 @@ Automated testing allows large teams of developers to maintain complex codebases
81
82
82
83
#### Getting started
83
84
84
-
[vue-test-utils](https://github.com/vuejs/vue-test-utils) is the official library for unit testing Vue components. The [vue-cli](https://github.com/vuejs/vue-cli) webpack template comes with either Karma or Jest, both well supported test runners, and there are some [guides](https://vue-test-utils.vuejs.org/en/guides/) in the `vue-test-utils` documentation.
85
+
[Vue Test Utils](https://github.com/vuejs/vue-test-utils) is the official library for unit testing Vue components. The [vue-cli](https://github.com/vuejs/vue-cli)`webpack` template comes with either Karma or Jest, both well supported test runners, and there are some [guides](https://vue-test-utils.vuejs.org/en/guides/) in the Vue Test Utils documentation.
85
86
86
87
## Real-World Example
87
88
88
-
Unit tests should be
89
+
Unit tests should be:
90
+
89
91
- Fast to run
90
92
- Easy to understand
91
93
- Only test a _single unit of work_
@@ -135,14 +137,15 @@ export default {
135
137
```
136
138
137
139
The things that we should test are:
140
+
138
141
- is the `message` rendered?
139
-
- if `error` is `true`, `<div class="error"`> should be present
140
-
- if `error` is `false`, `<div class="error"`> should not be present
142
+
- if `error` is `true`, `<div class="error">` should be present
143
+
- if `error` is `false`, `<div class="error">` should not be present
141
144
142
145
And our first attempt at test:
143
146
144
147
```js
145
-
import { shallow } from'vue-test-utils'
148
+
import { shallow } from'@vue/test-utils'
146
149
147
150
describe('Foo', () => {
148
151
it('renders a message and responds correctly to user input', () => {
@@ -226,9 +231,9 @@ The above test is fairly simple, but in practice Vue components often have other
226
231
- committing or dispatching mutations or actions with a `Vuex` store
227
232
- testing interaction
228
233
229
-
There are more complete examples showing such tests in the `vue-test-utils`[guides](https://vue-test-utils.vuejs.org/en/guides/).
234
+
There are more complete examples showing such tests in the Vue Test Utils[guides](https://vue-test-utils.vuejs.org/en/guides/).
230
235
231
-
`vue-test-utils` and the enormous JavaScript ecosystem provides plenty of tooling to facilitate almost 100% test coverage. Unit tests are only one part of the testing pyramid, though. Some other types of tests include e2e (end to end) tests, and snapshot tests. Unit tests are the smallest and most simple of tests - they make assertions on the smallest units of work, isolating each part of a single component.
236
+
Vue Test Utils and the enormous JavaScript ecosystem provides plenty of tooling to facilitate almost 100% test coverage. Unit tests are only one part of the testing pyramid, though. Some other types of tests include e2e (end to end) tests, and snapshot tests. Unit tests are the smallest and most simple of tests - they make assertions on the smallest units of work, isolating each part of a single component.
232
237
233
238
Snapshot tests save the markup of your Vue component, and compare to the new one generated each time the test runs. If something changes, the developer is notified, and can decide if the change was intentional (the component was updated) or accidentally (the component is behaving incorrectly).
0 commit comments