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
+73-67
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
---
2
-
title: Unit Testing Vue Components
2
+
title: Vue 组件的单元测试
3
3
type: cookbook
4
4
order: 6
5
5
---
6
6
7
-
## Simple Example
7
+
## 简单的示例
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.
The above code snippet shows how to test whether an error message is rendered based on the length of the username. It demonstrates the general idea of unit testing Vue components: render the component, and assert that the markup matches the state of the component.
- Provide documentation on how the component should behave
77
-
- Save time over testing manually
78
-
- Reduce bugs in new features
79
-
- Improve design
80
-
- Facilitate refactoring
75
+
组件的单元测试有很多好处:
81
76
82
-
Automated testing allows large teams of developers to maintain complex codebases.
77
+
- 提供描述组件行为的文档
78
+
- 节省手动测试的时间
79
+
- 减少研发新特性时产生的 bug
80
+
- 改进设计
81
+
- 促进重构
83
82
84
-
#### Getting started
83
+
自动化测试使得大团队中的开发者可以维护复杂的基础代码。
85
84
86
-
[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
+
#### 起步
87
86
88
-
## Real-World Example
87
+
[Vue Test Utils](https://github.com/vuejs/vue-test-utils) 是 Vue 组件单元测试的官方库。[Vue CLI](https://github.com/vuejs/vue-cli) 的 `webpack` 模板对 Karma 和 Jest 这两个测试运行器都支持,并且在 Vue Test Utils 的文档中有一些[引导](https://vue-test-utils.vuejs.org/zh-cn/guides/)。
89
88
90
-
Unit tests should be
91
-
- Fast to run
92
-
- Easy to understand
93
-
- Only test a _single unit of work_
89
+
## 实际的例子
94
90
95
-
Let's continue building on the previous example, while introducing the idea of a <ahref="https://en.wikipedia.org/wiki/Factory_(object-oriented_programming)">factory function</a> to make our test more compact and readable. The component should:
91
+
单元测试应该:
96
92
97
-
-show a 'Welcome to the Vue.js cookbook' greeting.
98
-
-prompt the user to enter their username
99
-
-display an error if the entered username is less than seven letters
- providing only the minimum data requires for the test
179
-
- refactoring duplicated logic (creating the `wrapper` and setting the `username` variable) into a factory function
186
+
*更新后的测试*:
180
187
181
-
*Updated test*:
182
188
```js
183
-
import { shallow } from'vue-test-utils'
189
+
import { shallow } from'@vue/test-utils'
184
190
importFoofrom'./Foo'
185
191
186
192
constfactory= (values= {}) => {
@@ -216,32 +222,32 @@ describe('Foo', () => {
216
222
})
217
223
```
218
224
219
-
Points to note:
225
+
注意事项:
220
226
221
-
At the top, we declare the factory function which merges the `values`object into `data`and returns a new `wrapper`instance. This way, we don't need to duplicate `const wrapper = shallow(Foo)` in every test. Another great benefit to this is when more complex components with a method or computed property you might want to mock or stub in every test, you only need to declare it once.
There are more complete examples showing such tests in the `vue-test-utils`[guides](https://vue-test-utils.vuejs.org/en/guides/).
237
+
我们在 Vue Test Utils 的[教程](https://vue-test-utils.vuejs.org/zh-cn/guides/)中提供了更完整的示例展示这些测试。
232
238
233
-
`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.
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).
End to end tests involve ensure a number of components interact together well. They are more high level. Some examples might be testing if a user can sign up, log in, and update their username. These are slowly to run than unit tests or snapshot tests.
Unit tests are most useful during development, either to help a developer think about how to design a component, or refactor an existing component, and are often run every time code is changed.
Higher level tests, such as end to end tests, run much slower. These usually run pre-deploy, to ensure each part of the system is working together correctly.
More information about testing Vue components can be found in [Testing Vue.js Applications](https://www.manning.com/books/testing-vuejs-applications) by core team member [Edd Yerburgh](https://eddyerburgh.me/).
Unit testing is an important part of any serious application. At first, when the vision of an application is not clear, unit testing might slow down development, but once a vision is established and real users will be interacting with the application, unit tests (and other types of automated tests) are absolutely essential to ensure the codebase is maintainable and scalable.
0 commit comments