Introduction
Testing Vue apps helps catch mistakes early and makes sure your app works as expected. It gives you confidence that changes won't break things.
Testing Vue apps helps catch mistakes early and makes sure your app works as expected. It gives you confidence that changes won't break things.
import { mount } from '@vue/test-utils' import MyComponent from './MyComponent.vue' test('renders message', () => { const wrapper = mount(MyComponent) expect(wrapper.text()).toContain('Hello') })
mount to create a test version of your Vue component.expect to check if the output matches what you want.import { mount } from '@vue/test-utils' import Button from './Button.vue' test('button click changes text', async () => { const wrapper = mount(Button) await wrapper.find('button').trigger('click') expect(wrapper.text()).toContain('Clicked') })
import { mount } from '@vue/test-utils' import Input from './Input.vue' test('input updates model', async () => { const wrapper = mount(Input) const input = wrapper.find('input') await input.setValue('Vue') expect(wrapper.vm.model).toBe('Vue') })
This test mounts the Message component with a prop and checks if it shows the right text.
import { mount } from '@vue/test-utils' import Message from './Message.vue' test('displays correct message', () => { const wrapper = mount(Message, { props: { text: 'Hello Vue' } }) expect(wrapper.text()).toBe('Hello Vue') })
Write small tests that check one thing at a time.
Run tests often to catch problems early.
Use descriptive test names to understand what each test does.
Testing helps find bugs before users do.
It makes your Vue app more reliable and easier to maintain.
Tests save time by preventing future errors.
const wrapper = mount(MyComponent)
expect(wrapper.text()).toContain('Welcome')const wrapper = mount(MyComponent)
expect(wrapper.text()).toContain('Hello')
expect(wrapper.find('button').click()).toBe(true)Pass the test and this pattern moves unknown