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
function mount(Component, options?: MountingOptions): VueWrapper
1605
+
```
1606
+
1607
+
**Details:**
1608
+
1609
+
`shallowMount` behaves exactly like `mount`, but it stubs all child components by default. Essentially, `shallowMount(Component)` is an alias of`mount(Component, { shallow: true })`.
1610
+
1611
+
## flushPromises
1612
+
1613
+
**Signature:**
1614
+
1615
+
```ts
1616
+
flushPromises(): Promise<unknown>
1617
+
```
1618
+
1619
+
**Details:**
1620
+
1621
+
`flushPromises` flushes al resolved promise handlers. This helps make sure async operations such as promises or DOM updates have happened before asserting against them.
1622
+
1623
+
Check out [Making HTTP requests](../guide/advanced/http-requests.md) to see an example of`flushPromises`in action.
Copy file name to clipboardExpand all lines: docs/guide/advanced/async-suspense.md
+29-20Lines changed: 29 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,9 @@
2
2
3
3
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?
4
4
5
-
You might know Vue updates reactively; when you change a value, the DOM is automatically updated to reflect the latest value. Vue does this _asynchronously_. In contrast, a test runner like Jest runs _synchronously_. This can cause some surprising results in tests. Let's look at some strategies to ensure Vue is updating the DOM as expected when we run our tests.
5
+
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.
6
+
7
+
Let's look at some strategies to ensure Vue is updating the DOM as expected when we run our tests.
6
8
7
9
## A Simple Example - Updating with `trigger`
8
10
@@ -11,7 +13,7 @@ Let's re-use the `<Counter>` component from [event handling](../essentials/event
11
13
```js
12
14
constCounter= {
13
15
template:`
14
-
Count: {{ count }}
16
+
<p>Count: {{ count }}</p>
15
17
<button @click="handleClick">Increment</button>
16
18
`,
17
19
data() {
@@ -39,11 +41,15 @@ test('increments by 1', () => {
39
41
})
40
42
```
41
43
42
-
Surprisingly, this fails! The reason is although `count` is increased, Vue will not update the DOM until the next "tick" or "render cycle". For this reason, the assertion will be called before Vue updates the DOM. This has to do with the concept of "macrotasks", "microtasks" and the JavaScript Event Loop. You can read more details and see a simple example [here](https://javascript.info/event-loop#macrotasks-and-microtasks).
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.
45
+
46
+
:::tip
47
+
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).
48
+
:::
43
49
44
-
Implementation details aside, how can we fix this? Vue actually provides a way for us to wait until the DOM is updated: `nextTick`:
50
+
Implementation details aside, how can we fix this? Vue actually provides a way for us to wait until the DOM is updated: `nextTick`.
Now the test will pass, because we ensure the next "tick" has executed updated the DOM before the assertion runs. Since `await nextTick()` is common, VTU provides a shortcut. Methods than cause the DOM to update, such as `trigger` and `setValue` return `nextTick`! So you can just `await` those directly:
65
+
Now the test will pass, because we ensure the next "tick" has executed updated the DOM before the assertion runs.
66
+
67
+
Since `await nextTick()` is common, Vue Test Utils provides a shortcut. Methods than cause the DOM to update, such as `trigger` and `setValue` return `nextTick`, so you can just `await` those directly:
`nextTick` is useful to ensure some change in reactivty 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. A common example is a function that returns a `Promise` that will lead to a change in the DOM. Perhaps you mocked your `axios` HTTP client using `jest.mock`:
81
+
`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.
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, you can use `[flush-promises](https://www.npmjs.com/package/flush-promises)`, which causes all outstanding promises to resolve immediately.
91
+
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.
0 commit comments