Performance: Testing async behavior
This affects the responsiveness and smoothness of user interactions during asynchronous operations in Vue components.
import { mount } from '@vue/test-utils'; import MyComponent from './MyComponent.vue'; test('good async test', async () => { const wrapper = mount(MyComponent); await wrapper.vm.fetchData(); await wrapper.vm.$nextTick(); expect(wrapper.text()).toContain('Loaded Data'); });
import { mount } from '@vue/test-utils'; import MyComponent from './MyComponent.vue'; test('bad async test', () => { const wrapper = mount(MyComponent); wrapper.vm.fetchData(); expect(wrapper.text()).toContain('Loaded Data'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Bad async test (no await) | Uncertain DOM state | Potential multiple reflows due to premature assertions | Unstable paint timing | [X] Bad |
| Good async test (await + nextTick) | Controlled DOM updates after async completion | Single reflow after data update | Stable paint after data load | [OK] Good |
await nextTick() after triggering an async update?await nextTick()await nextTick() waits for Vue to complete these DOM updates before assertions.wrapper.text() after the test runs?const wrapper = mount(MyComponent);
await wrapper.find('button').trigger('click');
await nextTick();
test('updates message after async call', async () => {
const wrapper = mount(MyComponent);
wrapper.vm.fetchData();
expect(wrapper.text()).toContain('Loaded');
});test('displays user name after fetch', async () => {
const wrapper = mount(UserComponent);
// What should you do here?
});flushPromises() waits for all pending promises like fetch to finish.await nextTick() ensures DOM updates are applied.Pass the test and this pattern moves unknown