javascript / beginner
Snippet
Unit Testing Component Props with Vue Test Utils
Vue Test Utils allows mounting components in an isolated test environment. Passing props through the mount options verifies that the component correctly renders dynamic input data.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
import { mount } from '@vue/test-utils';import { expect, test } from 'vitest';import UserGreeting from './UserGreeting.vue';test('renders greeting with passed username', () => {const wrapper = mount(UserGreeting, {props: { username: 'Alex' }});expect(wrapper.text()).toContain('Hello, Alex!');});
vue
Breakdown
1
import { mount } from '@vue/test-utils';
Imports the mount helper function to render the Vue component in tests.
2
const wrapper = mount(UserGreeting, { props: { username: 'Alex' } });
Mounts the UserGreeting component while passing 'Alex' as the username prop.
3
expect(wrapper.text()).toContain('Hello, Alex!');
Asserts that the rendered text inside the component includes the expected greeting.