Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update async-suspense.md #2034

Merged
merged 2 commits into from
May 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/guide/advanced/async-suspense.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,23 @@ const Async = defineComponent({
must be tested as follow:

```js
test('Async component', () => {
test('Async component', async () => {
const TestComponent = defineComponent({
components: { Async },
template: '<Suspense><Async/></Suspense>'
})

const wrapper = mount(TestComponent)
await flushPromises();
// ...
})
```
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`.

## Conclusion

- Vue updates the DOM asynchronously; tests runner executes code synchronously instead.
- Use `await nextTick()` to ensure the DOM has updated before the test continues.
- Functions that might update the DOM (like `trigger` and `setValue`) return `nextTick`, so you need to `await` them.
- Use `flushPromises` from Vue Test Utils to resolve any unresolved promises from non-Vue dependencies (such as API requests).
- Use `Suspense` to test components with an asynchronous `setup`.
- Use `Suspense` to test components with an asynchronous `setup` in an asynchronous test function.