8000 Update unit-testing-vue-components.md (#1505) · lipis/vuejs.org@2d0bed0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d0bed0

Browse files
Jinjiangsdras
authored andcommitted
Update unit-testing-vue-components.md (vuejs#1505)
* Update unit-testing-vue-components.md * Update unit-testing-vue-components.md
1 parent fc18488 commit 2d0bed0

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/v2/cookbook/unit-testing-vue-components.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ order: 6
66

77
## Simple Example
88

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.
1010

1111
This simple example tests whether some text is rendered:
1212

@@ -44,13 +44,13 @@ export default {
4444
```
4545

4646
```js
47-
import { shallow } from 'vue-test-utils'
47+
import { shallow } from '@vue/test-utils'
4848

4949
test('Foo', () => {
5050
// render the component
5151
const wrapper = shallow(Hello)
5252

53-
// should not allow for username less than 7 characters, excludes whitespace
53+
// should not allow for `username` less than 7 characters, excludes whitespace
5454
wrapper.setData({ username: ' '.repeat(7) })
5555

5656
// assert the error is rendered
@@ -71,6 +71,7 @@ The above code snippet shows how to test whether an error message is rendered ba
7171
## Why test?
7272

7373
Component unit tests have lots of benefits:
74+
7475
- Provide documentation on how the component should behave
7576
- Save time over testing manually
7677
- Reduce bugs in new features
@@ -81,11 +82,12 @@ Automated testing allows large teams of developers to maintain complex codebases
8182

8283
#### Getting started
8384

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.
8586

8687
## Real-World Example
8788

88-
Unit tests should be
89+
Unit tests should be:
90+
8991
- Fast to run
9092
- Easy to understand
9193
- Only test a _single unit of work_
@@ -135,14 +137,15 @@ export default {
135137
```
136138

137139
The things that we should test are:
140+
138141
- 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
141144

142145
And our first attempt at test:
143146

144147
```js
145-
import { shallow } from 'vue-test-utils'
148+
import { shallow } from '@vue/test-utils'
146149

147150
describe('Foo', () => {
148151
it('renders a message and responds correctly to user input', () => {
@@ -159,26 +162,28 @@ describe('Foo', () => {
159162
// assert the error is rendered
160163
expect(wrapper.find('.error').exists()).toBeTruthy()
161164

162-
// update the username and assert error is longer rendered
163-
wrapper.setData({ username: 'Lachlan' })
165+
// update the `username` and assert error is longer rendered
166+
wrapper.setData({ username: 'Lachlan' })
164167
expect(wrapper.find('.error').exists()).toBeFalsy()
165168
})
166169
})
167170
```
168171

169172
There are some problems with the above:
173+
170174
- a single test is making assertions about different things
171175
- difficult to tell the different states the component can be in, and what should be rendered
172176

173177
The below example improves the test by:
178+
174179
- only making one assertion per `it` block
175180
- having short, clear test descriptions
176181
- providing only the minimum data requires for the test
177182
- refactoring duplicated logic (creating the `wrapper` and setting the `username` variable) into a factory function
178183

179184
*Updated test*:
180185
```js
181-
import { shallow } from 'vue-test-utils'
186+
import { shallow } from '@vue/test-utils'
182187
import Foo from './Foo'
183188

184189
const factory = (values = {}) => {
@@ -201,7 +206,7 @@ describe('Foo', () => {
201206
})
202207

203208
it('renders an error when username is whitespace', () => {
204-
const wrapper = factory({ username: ' '.repeat(7) })
209+
const wrapper = factory({ username: ' '.repeat(7) })
205210

206211
expect(wrapper.find('.error').exists()).toBeTruthy()
207212
})
@@ -226,9 +231,9 @@ The above test is fairly simple, but in practice Vue components often have other
226231
- committing or dispatching mutations or actions with a `Vuex` store
227232
- testing interaction
228233

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/).
230235

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.
232237

233238
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).
234239

0 commit comments

Comments
 (0)
0