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
You may have noticed some other parts of the guide using `await` when calling some methods on `wrapper`, such as `trigger`and`setValue`. What's that all about?
You might know Vue updates reactively: when you change a value, the DOM is automatically updated to reflect the latest value. [Vue does these updates asynchronously](https://v3.vuejs.org/guide/change-detection.html#async-update-queue). In contrast, a test runner like Jest runs _synchronously_. This can cause some surprising results in tests.
5
+
你可能知道 [Vue 是以响应式的方式更新的](https://v3.vuejs.org/guide/change-detection.html#async-update-queue):当你更改一个值时,DOM 会自动更新以反映最新的值。Vue 的这些更新是异步进行的。与此相对,像 Jest 这样的测试运行器是 _同步_ 的。这可能会导致测试中出现一些意外的结果。
6
6
7
-
Let's look at some strategies to ensure Vue is updating the DOM as expected when we run our tests.
7
+
让我们看看一些策略,以确保在运行测试时 Vue 按预期更新 DOM。
8
8
9
-
## A Simple Example - Updating with `trigger`
9
+
## 简单示例 - 使用 `trigger` 更新
10
10
11
-
Let's re-use the `<Counter>` component from [event handling](../essentials/event-handling)with one change; we now render the `count` in the `template`.
Let's write a test to verify the `count`is increasing:
32
+
让我们写一个测试来验证 `count`是否在增加:
33
33
34
34
```js
35
35
test('increments by 1', () => {
@@ -41,13 +41,13 @@ test('increments by 1', () => {
41
41
})
42
42
```
43
43
44
-
Surprisingly, this fails! The reason is although `count`is increased, Vue will not update the DOM until the next event loop tick. For this reason, the assertion (`expect()...`) will be called before Vue updates the DOM.
If you want to learn more about this core JavaScript behavior, read about the [Event Loop and its macrotasks and microtasks](https://javascript.info/event-loop#macrotasks-and-microtasks).
Now the test will pass because we ensure the next "tick" has been executed and the DOM has been updated before the assertion runs.
65
+
现在测试将通过,因为我们确保在断言运行之前,下一个 "tick" 已经执行并且 DOM 已经更新。
66
66
67
-
Since`await nextTick()`is common, Vue Test Utils provides a shortcut. Methods that cause the DOM to update, such as `trigger`and`setValue`return`nextTick`, so you can just `await`those directly:
67
+
由于`await nextTick()`是常见的,Vue Test Utils 提供了一个快捷方式。导致 DOM 更新的方法,如 `trigger`和`setValue`返回`nextTick`,因此你可以直接 `await`它们:
`nextTick`is useful to ensure some change in reactive data is reflected in the DOM before continuing the test. However, sometimes you may want to ensure other, non Vue-related asynchronous behavior is completed, too.
81
+
`nextTick`用于确保某个响应式数据的变化在继续测试之前反映在 DOM 中。然而,有时你可能还想确保其他与 Vue 无关的异步行为也已完成。
82
82
83
-
A common example is a function that returns a `Promise`. Perhaps you mocked your `axios` HTTP client using `jest.mock`:
In this case, Vue has no knowledge of the unresolved Promise, so calling `nextTick`will not work - your assertion may run before it is resolved. For scenarios like this, Vue Test Utils exposes[`flushPromises`](../../api/#flushPromises), which causes all outstanding promises to resolve immediately.
Note: To access your `Async` components' underlying `vm` instance, use the return value of `wrapper.findComponent(Async)`. Since a new component is defined and mounted in this scenario, the wrapper returned by `mount(TestComponent)` contains its' own (empty) `vm`.
Copy file name to clipboardExpand all lines: docs/zh/guide/advanced/component-instance.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
-
# Component Instance
1
+
# 组件实例
2
2
3
-
[`mount`](/api/#mount)returns a `VueWrapper` with lots of convenient methods for testing Vue components. Sometimes you might want access to the underlying Vue instance. You can access that with the `vm`property.
Here is a simple component that combines props and data to render a greeting:
7
+
下面是一个简单的组件,它结合了 props 和 data 来渲染一个问候语:
8
8
9
9
```ts
10
10
test('renders a greeting', () => {
@@ -28,7 +28,7 @@ test('renders a greeting', () => {
28
28
})
29
29
```
30
30
31
-
Let's take a look at what's available on `vm` by with `console.log(wrapper.vm)`:
31
+
让我们通过 `console.log(wrapper.vm)` 来查看 `vm` 上可用的内容:
32
32
33
33
```js
34
34
{
@@ -38,13 +38,13 @@ Let's take a look at what's available on `vm` by with `console.log(wrapper.vm)`:
38
38
}
39
39
```
40
40
41
-
We can see both `msg1`and`msg2`! Things like `methods`and`computed`properties will show up too, if they are defined. When writing a test, while it's generally recommended to assert against the DOM (using something like `wrapper.html()`), in some rare circumstances you might need access to the underlying Vue instance.
41
+
我们可以看到 `msg1`和`msg2`!如果定义了 `methods`和`computed`属性,它们也会显示出来。在编写测试时,虽然通常建议对 DOM 进行断言(使用 `wrapper.html()` 等),但在一些特殊情况下,你可能需要访问底层的 Vue 实例。
42
42
43
-
## Usage with `getComponent`and`findComponent`
43
+
## 与 `getComponent`和`findComponent` 的使用
44
44
45
-
`getComponent`and`findComponent`return a `VueWrapper` - much like the one get from `mount`. This means you can also access all the same properties, including `vm`, on the result of `getComponent` or `findComponent`.
A more thorough way to test this would be asserting against the rendered content. Doing this means you asserts the correct prop is passed *and* rendered.
68
+
一种更彻底的测试方式是对渲染的内容进行断言。这样可以确保正确的 prop 被传递*并*渲染。
69
69
70
-
:::warning WrapperLike type when using CSS selector
71
-
When using `wrapper.findComponent('.foo')` for example then VTU will return the `WrapperLike` type. This is because functional components
72
-
would need a `DOMWrapper` otherwise a `VueWrapper`. You can force to return a `VueWrapper` by providing the correct component type:
0 commit comments