Testing in Vue 3
Testing ensures your Vue 3 application is reliable, maintainable, and bug-free. Use a combination of unit, integration, and end-to-end tests.
Unit Testing
- Use Vitest or Jest for unit tests.
- Test components, composables, and stores in isolation.
- Use Vue Test Utils for rendering and interacting with components.
Example: Component Unit Test (Vitest)
import { mount } from '@vue/test-utils'
import Counter from '@/components/Counter.vue'
test('increments value on click', async () => {
const wrapper = mount(Counter)
await wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('1')
})
Integration Testing
- Test interactions between components and services.
- Mock API calls and external dependencies.
End-to-End (E2E) Testing
- Use Cypress or Playwright for E2E tests.
- Simulate real user flows (navigation, form submission, etc.).
Best Practices
- Write tests for critical logic and user flows.
- Run tests in CI pipelines for every commit/PR.
- Use coverage tools to monitor test completeness.