We test props and events to make sure components get the right data and communicate correctly.
Testing props and events in Vue
import { mount } from '@vue/test-utils' const wrapper = mount(Component, { props: { propName: value } }) wrapper.vm.$emit('eventName', eventData) expect(wrapper.props('propName')).toBe(value) expect(wrapper.emitted('eventName')).toBeTruthy()
Use mount to create a test instance of your component.
Check props with wrapper.props() and events with wrapper.emitted().
label prop is passed correctly to the MyButton component.const wrapper = mount(MyButton, { props: { label: 'Click me' } }) expect(wrapper.props('label')).toBe('Click me')
click event.await wrapper.find('button').trigger('click') expect(wrapper.emitted('click')).toBeTruthy()
submit event was emitted with the correct data.expect(wrapper.emitted('submit')[0]).toEqual([{ name: 'John' }])
This component shows a button with a name prop. When clicked, it emits a greet event with a message. The test checks the prop and event.
import { mount } from '@vue/test-utils' import { defineComponent, h } from 'vue' const Greeting = defineComponent({ props: { name: String }, emits: ['greet'], setup(props, { emit }) { const sayHello = () => emit('greet', `Hello, ${props.name}!`) return () => h('button', { onClick: sayHello }, `Greet ${props.name}`) } }) // Test const wrapper = mount(Greeting, { props: { name: 'Alice' } }) // Check prop console.log(wrapper.props('name')) // Trigger event wrapper.find('button').trigger('click') // Check event console.log(wrapper.emitted('greet'))
Always test both props and events to cover input and output of components.
Use await with trigger if your event handler is async.
Check event payloads carefully to ensure correct data is sent.
Props pass data into components; test them to confirm correct values.
Events send messages out; test them to confirm correct triggers and data.
Use Vue Test Utils mount, props(), and emitted() for easy testing.
Practice
Solution
Step 1: Understand what props are in Vue
Props are how a parent component passes data to a child component.Step 2: Identify the goal of testing props
Testing props ensures the child component receives the right data as expected.Final Answer:
To check if the component receives the correct data from its parent -> Option AQuick Check:
Props test data passing = C [OK]
- Confusing props testing with state testing
- Thinking props test styles or rendering only
- Assuming props test event emissions
Solution
Step 1: Recall Vue Test Utils methods for events
The methodemitted()returns an object of events emitted by the component.Step 2: Differentiate from other methods
trigger()simulates DOM events,props()reads props, andsetData()changes component data.Final Answer:
emitted() -> Option CQuick Check:
Check emitted events = emitted() = B [OK]
- Using trigger() to check emitted events
- Confusing props() with emitted()
- Trying to use setData() for event checking
const wrapper = mount(MyButton, { props: { label: 'Click' } })
await wrapper.find('button').trigger('click')
const events = wrapper.emitted()What will
events contain if MyButton emits a 'click' event on button click?Solution
Step 1: Understand emitted() output format
emitted() returns an object where keys are event names and values are arrays of argument arrays for each emit.Step 2: Analyze the event emission
If the component emits 'click' without arguments, emitted().click is an array with empty arrays for each emit.Final Answer:
{ click: [ [ ] ] } -> Option DQuick Check:
Event emitted without args = empty array inside emitted() = D [OK]
- Assuming emitted event has string argument by default
- Confusing emitted() output structure
- Expecting emitted() to return flat arrays
const wrapper = mount(MyInput, { props: { value: 'test' } })
wrapper.find('input').trigger('input', { target: { value: 'new' } })
expect(wrapper.emitted('input')[0]).toEqual(['new'])Solution
Step 1: Check if trigger() needs awaiting
In Vue Test Utils, trigger() returns a promise and should be awaited to ensure DOM updates.Step 2: Verify test correctness
Without await, the emitted event may not be registered before the expect runs, causing test failure.Final Answer:
trigger() should be awaited because it returns a promise -> Option AQuick Check:
Always await trigger() for event emission tests [OK]
- Not awaiting trigger() causing false negatives
- Mixing up prop names for v-model
- Expecting event payload as object instead of emitted args
Solution
Step 1: Understand conditional event emission
The component emits 'submit' only if prop 'isValid' is true, so test must check both true and false cases.Step 2: Design test cases accordingly
Mount with isValid false and trigger submit to confirm no event; then mount with true and trigger submit to confirm event emitted.Final Answer:
Mount component with isValid=false, trigger submit, expect no emitted event; then mount with isValid=true, trigger submit, expect emitted event -> Option BQuick Check:
Test both prop states for conditional event emission [OK]
- Testing only one prop state
- Expecting event without triggering action
- Ignoring prop effect on event emission
Ready to prove it?
Pass the test and this pattern moves unknown
