File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -114,9 +114,40 @@ test('uses a mocked axios HTTP client and flushPromises', async () => {
114114If 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 ` .
You can’t perform that action at this time.
0 commit comments