Performance: Component testing with Vue Test Utils
Component testing affects development speed and feedback loop but can indirectly impact runtime performance by catching inefficient rendering patterns early.
import { mount } from '@vue/test-utils' import MyComponent from './MyComponent.vue' test('renders with final prop value only', async () => { const wrapper = mount(MyComponent) await wrapper.setProps({ count: 99 }) expect(wrapper.text()).toContain('99') })
import { mount } from '@vue/test-utils' import MyComponent from './MyComponent.vue' test('renders and updates DOM repeatedly', async () => { const wrapper = mount(MyComponent) for(let i = 0; i < 100; i++) { await wrapper.setProps({ count: i }) } expect(wrapper.text()).toContain('99') })
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple prop updates in loop | Many DOM patches | 100 reflows | High paint cost | [X] Bad |
| Single prop update before assertion | One DOM patch | 1 reflow | Low paint cost | [OK] Good |
mount() from Vue Test Utils in component testing?mount()mount() creates a real instance of a Vue component in a test environment.
mount() is NOT forIt is not used for compiling, styling, or deploying components.
mount from Vue Test Utils in a test file?The correct import uses named import from '@vue/test-utils'.
import mount from 'vue-test-utils'; uses default import incorrectly, C uses CommonJS require which is outdated here, A imports from wrong package.
const wrapper = mount({ template: '{{ count }}', data() { return { count: 0 } } });
await wrapper.find('button').trigger('click');
console.log(wrapper.text());const wrapper = mount(MyComponent);
wrapper.find('button').trigger('click');
expect(wrapper.text()).toContain('Clicked');submit with a payload object when a form is submitted. Which Vue Test Utils method correctly checks the event and its payload?submit event payload matches the expected object, which is correct.Pass the test and this pattern moves unknown