Vue.js Developer interview question · Most asked
How do you test Vue components?
Summary
Use Vitest with Vue Test Utils (or Testing Library for Vue) to mount components, interact with them as a user would, and assert on rendered output and emitted events; test composables and stores as plain functions; and cover critical journeys end to end with Playwright or Cypress.
Longer Answer
Prefer testing behaviour over implementation details: find elements by role or text, trigger events, await updates (await wrapper.find('button').trigger('click') or await nextTick()), and assert what the user sees. Stub child components or network calls (MSW) where needed, use createTestingPinia for stores, and provide router and plugins via global mounting options.
Key Points
- Vitest + Vue Test Utils or Testing Library.
- Test behaviour, not internals.
- Composables and stores as functions.
- E2E for critical flows.
More most-asked Vue.js Developer questions
- Options API or Composition API: what is the difference, and which would you use?
- How do props work, and what is one-way data flow?
- How does Vue 3's reactivity system work, and how does it differ from Vue 2's?
- computed, watch or watchEffect: when do you use each?
- ref or reactive: what is the difference, and when do you use each?
- How do you use Vue Router: dynamic routes, nested routes and navigation guards?
- Why does v-for need a key, and why should it not be the index?
- What are composables, and why did they replace mixins?