Snapshot testing helps you check if your Vue components look the same as before. It catches unexpected changes easily.
Snapshot testing in Vue
import { mount } from '@vue/test-utils' import MyComponent from '@/components/MyComponent.vue' test('matches snapshot', () => { const wrapper = mount(MyComponent) expect(wrapper.html()).toMatchSnapshot() })
Use mount from @vue/test-utils to render the component.
toMatchSnapshot() saves the HTML output and compares it on future runs.
import { mount } from '@vue/test-utils' import Button from '@/components/Button.vue' test('Button snapshot', () => { const wrapper = mount(Button) expect(wrapper.html()).toMatchSnapshot() })
import { mount } from '@vue/test-utils' import List from '@/components/List.vue' test('List snapshot with props', () => { const wrapper = mount(List, { props: { items: ['apple', 'banana'] } }) expect(wrapper.html()).toMatchSnapshot() })
This test mounts a simple HelloWorld component and checks its HTML output against a saved snapshot. If the output changes, the test will fail, alerting you to unexpected UI changes.
import { mount } from '@vue/test-utils' import { describe, test, expect } from 'vitest' // Simple Vue component const HelloWorld = { template: `<div><h1>Hello, Vue!</h1></div>` } describe('HelloWorld snapshot test', () => { test('renders correctly', () => { const wrapper = mount(HelloWorld) expect(wrapper.html()).toMatchSnapshot() }) })
Snapshots are saved in a special folder and should be committed to your code repository.
If your component changes intentionally, update the snapshot by running the test command with the update flag (e.g., vitest -u).
Snapshot tests are fast but should be combined with other tests for full coverage.
Snapshot testing saves your component's HTML output to catch unexpected changes.
Use mount from @vue/test-utils and toMatchSnapshot() in your tests.
Update snapshots intentionally when your UI changes as planned.
Practice
Solution
Step 1: Understand snapshot testing purpose
Snapshot testing captures the rendered HTML output of a component to detect changes over time.Step 2: Compare with other testing types
Unlike method or event testing, snapshot testing focuses on UI structure, not behavior or styles.Final Answer:
To save the component's rendered HTML and detect unexpected UI changes -> Option BQuick Check:
Snapshot testing = UI output check [OK]
- Confusing snapshot testing with unit testing methods
- Thinking it tests CSS or events directly
- Assuming it tests component logic
Solution
Step 1: Identify correct mounting and snapshot method
Usingmountfrom@vue/test-utilsand callinghtml()returns the rendered HTML string.Step 2: Match snapshot with Jest matcher
toMatchSnapshot()is the correct Jest matcher to compare the HTML output to the saved snapshot.Final Answer:
expect(mount(MyComponent).html()).toMatchSnapshot() -> Option AQuick Check:
mount + html() + toMatchSnapshot() = correct syntax [OK]
- Using shallowMount instead of mount when full HTML is needed
- Trying to snapshot the component instance (vm) instead of HTML
- Using incorrect matcher like toBeSnapshot()
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'
test('renders correctly', () => {
const wrapper = mount(MyComponent)
expect(wrapper.html()).toMatchSnapshot()
})Solution
Step 1: Understand snapshot comparison behavior
When the component's HTML changes, Jest compares the new output to the saved snapshot and detects differences.Step 2: Result of mismatch
If the HTML differs unexpectedly, the test fails to alert the developer about the change.Final Answer:
The test will fail indicating the snapshot does not match the new HTML -> Option AQuick Check:
Snapshot mismatch causes test failure [OK]
- Assuming tests auto-update snapshots without command
- Thinking test passes even if HTML changes
- Confusing syntax errors with snapshot failures
expect(wrapper.html().toMatchSnapshot())What is the problem and how to fix it?
Solution
Step 1: Identify incorrect method chaining
The code callstoMatchSnapshot()on the string result ofwrapper.html(), which is incorrect.Step 2: Correct usage of Jest matcher
The matchertoMatchSnapshot()must be called on theexpect()wrapper, not on the string itself.Final Answer:
toMatchSnapshot() should be called on expect(), not on the string; fix: expect(wrapper.html()).toMatchSnapshot() -> Option DQuick Check:
Call matcher on expect() result [OK]
- Calling matcher on string instead of expect()
- Using wrong matcher name
- Assuming wrapper.html() returns undefined
Solution
Step 1: Understand conditional rendering impact
The component's output changes based on the prop, so each state has a different HTML structure.Step 2: Write separate snapshot tests per state
Mount the component twice with different prop values and snapshot each output separately to catch changes in both states.Final Answer:
Write two separate tests, each mounting the component with different prop values, and snapshot each output -> Option CQuick Check:
Separate snapshots for different states [OK]
- Trying to snapshot multiple states in one test
- Not passing props to trigger conditional rendering
- Updating snapshots without verifying changes
Ready to prove it?
Pass the test and this pattern moves unknown
