|
1 |
| -# Suspense and Async Behavior |
| 1 | +# Asynchronous Behavior |
2 | 2 |
|
3 |
| -`async` related stuff. Maybe `flushPromises`? |
| 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 | + |
| 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. |
| 6 | + |
| 7 | +## A Simple Example - Updating with `trigger` |
| 8 | + |
| 9 | +Let's re-use the `<Counter>` component from [event handling](/guide/event-handling) with one change; we now render the `count` in the `template`. |
| 10 | + |
| 11 | +```js |
| 12 | +const Counter = { |
| 13 | + template: ` |
| 14 | + Count: {{ count }} |
| 15 | + <button @click="handleClick">Increment</button> |
| 16 | + `, |
| 17 | + data() { |
| 18 | + return { |
| 19 | + count: 0 |
| 20 | + } |
| 21 | + }, |
| 22 | + methods: { |
| 23 | + handleClick() { |
| 24 | + this.count += 1 |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +Let's write a test to verify the `count` is increasing: |
| 31 | + |
| 32 | +```js |
| 33 | +test('increments by 1', () => { |
| 34 | + const wrapper = mount(Counter) |
| 35 | + |
| 36 | + wrapper.find('button').trigger('click') |
| 37 | + |
| 38 | + expect(wrapper.html()).toContain('Count: 1') |
| 39 | +}) |
| 40 | +``` |
| 41 | + |
| 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 "marcotasks", "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). |
| 43 | + |
| 44 | +Implementation details aside, how can we fix this? Vue actually provides a way for us to wait until the DOM is updated: `nextTick`: |
| 45 | + |
| 46 | +```js {7} |
| 47 | +import { nextTick } from 'vue' |
| 48 | + |
| 49 | +test('increments by 1', async () => { |
| 50 | + const wrapper = mount(Counter) |
| 51 | + |
| 52 | + wrapper.find('button').trigger('click') |
| 53 | + await nextTick() |
| 54 | + |
| 55 | + expect(wrapper.html()).toContain('Count: 1') |
| 56 | +}) |
| 57 | +``` |
| 58 | + |
| 59 | +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: |
| 60 | + |
| 61 | +```js {4} |
| 62 | +test('increments by 1', async () => { |
| 63 | + const wrapper = mount(Counter) |
| 64 | + |
| 65 | + await wrapper.find('button').trigger('click') |
| 66 | + |
| 67 | + expect(wrapper.html()).toContain('Count: 1') |
| 68 | +}) |
| 69 | +``` |
| 70 | + |
| 71 | +## Resolving Other Asynchronous Behavior |
| 72 | + |
| 73 | +`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`: |
| 74 | + |
| 75 | +```js |
| 76 | +jest.mock('axios', () => ({ |
| 77 | + get: () => Promise.resolve({ data: 'some mocked data!' }) |
| 78 | +})) |
| 79 | +``` |
| 80 | + |
| 81 | +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. |
| 82 | + |
| 83 | +Let's see an example: |
| 84 | + |
| 85 | +```js |
| 86 | +import flushPromises from 'flush-promises' |
| 87 | +import axios from 'axios' |
| 88 | + |
| 89 | +jest.mock('axios', () => ({ |
| 90 | + get: () => new Promise(resolve => { |
| 91 | + resolve({ data: 'some mocked data!' }) |
| 92 | + }) |
| 93 | +})) |
| 94 | + |
| 95 | + |
| 96 | +test('uses a mocked axios HTTP client and flush-promises', async () => { |
| 97 | + // some component that makes a HTTP called in `created` using `axios` |
| 98 | + const wrapper = mount(AxiosComponent) |
| 99 | + |
| 100 | + await flushPromses() // axios promise is resolved immediately! |
| 101 | + |
| 102 | + // assertions! |
| 103 | +}) |
| 104 | + |
| 105 | +``` |
| 106 | + |
| 107 | +> If you haven't tested Components with API requests before, you can learn more in [HTTP Requests](/guide/http-requests). |
| 108 | +## Conclusion |
| 109 | + |
| 110 | +- Vue updates the DOM asynchronously; tests runner execute code synchronously. |
| 111 | +- Use `await nextTick()` to ensure the DOM has updated before the test continues |
| 112 | +- Functions that might update the DOM, like `trigger` and `setValue` return `nextTick`, so you should `await` them. |
| 113 | +- Use `flush-promises` to resolve any unresolved promises from non-Vue dependencies. |
0 commit comments