Component testing helps you check if your Vue parts work right. It finds mistakes early so your app stays strong.
Component testing with Vue Test Utils
import { mount } from '@vue/test-utils' import ComponentName from '@/components/ComponentName.vue' test('description of test', () => { const wrapper = mount(ComponentName) // interact with wrapper or check output expect(wrapper.text()).toContain('expected text') })
mount() creates a full instance of the component for testing.
Use expect() to check if the output or behavior matches what you want.
import { mount } from '@vue/test-utils' import HelloWorld from '@/components/HelloWorld.vue' test('shows greeting message', () => { const wrapper = mount(HelloWorld) expect(wrapper.text()).toContain('Hello') })
import { mount } from '@vue/test-utils' import ButtonCounter from '@/components/ButtonCounter.vue' test('increments count on click', async () => { const wrapper = mount(ButtonCounter) await wrapper.find('button').trigger('click') expect(wrapper.text()).toContain('Count: 1') })
import { mount } from '@vue/test-utils' import EmitEvent from '@/components/EmitEvent.vue' test('emits custom event on action', async () => { const wrapper = mount(EmitEvent) await wrapper.find('button').trigger('click') expect(wrapper.emitted()).toHaveProperty('custom-event') })
This example tests a simple counter button. The first test checks it starts at zero. The second test clicks the button and checks the count increases to 1.
import { mount } from '@vue/test-utils' import { describe, test, expect } from 'vitest' // Simple Vue component const Counter = { template: `<button @click=\"count++\">Count: {{ count }}</button>`, data() { return { count: 0 } } } describe('Counter component', () => { test('starts at zero', () => { const wrapper = mount(Counter) expect(wrapper.text()).toBe('Count: 0') }) test('increments count when clicked', async () => { const wrapper = mount(Counter) await wrapper.find('button').trigger('click') expect(wrapper.text()).toBe('Count: 1') }) })
Use await with trigger to wait for Vue to update after events.
Mounting a component creates a real instance, so tests are close to how users see it.
Check emitted events with wrapper.emitted() to test communication between components.
Component testing checks if Vue parts behave as expected.
Use Vue Test Utils mount() to create a test instance.
Simulate user actions and check output or events to confirm correct behavior.
Practice
mount() from Vue Test Utils in component testing?Solution
Step 1: Understand the role of
mount()mount()creates a real instance of a Vue component in a test environment.Step 2: Identify what
mount()is NOT forIt is not used for compiling, styling, or deploying components.
Final Answer:
To create a full instance of a Vue component for testing its behavior -> Option DQuick Check:
mount() creates component instance [OK]
- Confusing mount() with build or deploy tools
- Thinking mount() styles components
- Assuming mount() compiles code
mount from Vue Test Utils in a test file?Solution
Step 1: Check the official import syntax
The correct import uses named import from '@vue/test-utils'.
Step 2: Identify incorrect import styles
import mount from 'vue-test-utils'; uses default import incorrectly, C uses CommonJS require which is outdated here, A imports from wrong package.
Final Answer:
import { mount } from '@vue/test-utils'; -> Option AQuick Check:
Correct import syntax = import { mount } from '@vue/test-utils'; [OK]
- Using default import instead of named import
- Using require() instead of import
- Importing from wrong package
const wrapper = mount({ template: '{{ count }}', data() { return { count: 0 } } });
await wrapper.find('button').trigger('click');
console.log(wrapper.text());What will be logged to the console?
Solution
Step 1: Understand the component behavior
The button shows the current count, starting at 0, and increments count on click.Step 2: Analyze the test actions
The test triggers a click event on the button, increasing count from 0 to 1, then logs the button text.Final Answer:
1 -> Option CQuick Check:
Click increments count, text shows 1 [OK]
- Assuming text stays 0 after click
- Confusing event name with text output
- Forgetting to await trigger for update
const wrapper = mount(MyComponent);
wrapper.find('button').trigger('click');
expect(wrapper.text()).toContain('Clicked');Solution
Step 1: Identify asynchronous DOM update
Triggering events may update DOM asynchronously, so awaiting trigger is needed.Step 2: Check the code for await usage
The code calls trigger() without await, so the expect() runs before DOM updates.Final Answer:
Missing await before trigger to wait for DOM update -> Option AQuick Check:
Await trigger() for DOM update [OK]
- Not awaiting trigger causing test to fail
- Assuming find() selector is wrong without checking
- Forgetting to import mount (not shown here)
- Misusing expect() matcher syntax
submit with a payload object when a form is submitted. Which Vue Test Utils method correctly checks the event and its payload?Solution
Step 1: Understand event emission checking
Vue Test Utils stores emitted events in wrapper.emitted(), accessible by event name and call index.Step 2: Verify correct assertion for event payload
expect(wrapper.emitted().submit[0]).toEqual([{ name: 'John' }]); checks the firstsubmitevent payload matches the expected object, which is correct.Final Answer:
expect(wrapper.emitted().submit[0]).toEqual([{ name: 'John' }]); -> Option BQuick Check:
Use emitted() to check event and payload [OK]
- Checking text instead of emitted events
- Testing props instead of events
- Not accessing emitted event payload array
Ready to prove it?
Pass the test and this pattern moves unknown
