Skip to content

Commit a56375f

Browse files
authored
docs: async setup testing (#1215)
Minimal explanation of how to test a component with an async `setup`, by mounting it inside a `Suspense`. Fixes #1207
1 parent 5bfe95c commit a56375f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/guide/advanced/async-suspense.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,40 @@ test('uses a mocked axios HTTP client and flushPromises', async () => {
114114
If you want to learn more about testing requests on Components, make sure you check [Making HTTP Requests](http-requests.md) guide.
115115
:::
116116

117+
## Testing asynchronous `setup`
118+
119+
If the component you want to test uses an asynchronous `setup`,
120+
then you must mount the component inside a `Suspense` component
121+
(as you do when you use it in your application).
122+
123+
For example, this `Async` component:
124+
125+
```js
126+
const Async = defineComponent({
127+
async setup() {
128+
// await something
129+
}
130+
})
131+
```
132+
133+
must be tested as follow:
134+
135+
```js
136+
test('Async component', () => {
137+
const TestComponent = defineComponent({
138+
components: { Async },
139+
template: '<Suspense><Async/></Suspense>'
140+
})
141+
142+
const wrapper = mount(TestComponent)
143+
// ...
144+
})
145+
```
146+
117147
## Conclusion
118148

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

0 commit comments

Comments
 (0)