0
0
Vueframework~10 mins

Snapshot testing in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Snapshot testing
Write Component Code
Render Component in Test
Generate Snapshot File
Compare Rendered Output to Snapshot
Test Passes
Update Snapshot or Fix Code
Snapshot testing captures the component's output and compares it to a saved snapshot to detect unexpected changes.
Execution Sample
Vue
import { mount } from '@vue/test-utils';
import MyButton from './MyButton.vue';

test('renders correctly', () => {
  const wrapper = mount(MyButton);
  expect(wrapper.html()).toMatchSnapshot();
});
This test mounts a Vue component and checks if its HTML matches the saved snapshot.
Execution Table
StepActionRendered OutputSnapshot Exists?Comparison ResultTest Outcome
1Mount MyButton component<button>Click me</button>NoN/ACreate new snapshot, Test Passes
2Run test again<button>Click me</button>YesMatchTest Passes
3Change component text to 'Press me'<button>Press me</button>YesMismatchTest Fails
4Update snapshot after change<button>Press me</button>YesMatchTest Passes
💡 Test stops after snapshot matches or mismatch is handled by update or fix.
Variable Tracker
VariableStartAfter 1After 2After 3Final
Rendered OutputN/A<button>Click me</button><button>Click me</button><button>Press me</button><button>Press me</button>
Snapshot ExistsNoYesYesYesYes
Comparison ResultN/AN/AMatchMismatchMatch
Test OutcomeN/APassPassFailPass
Key Moments - 3 Insights
Why does the test fail when the component text changes?
Because the rendered output no longer matches the saved snapshot, as shown in execution_table row 3 where Comparison Result is 'Mismatch'.
What happens if there is no existing snapshot?
A new snapshot is created from the rendered output, and the test passes, as shown in execution_table row 1.
How do you fix a failing snapshot test after intentional changes?
You update the snapshot to match the new output, then the test passes, as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the test outcome at step 3 when the component text changes?
ASnapshot Created
BTest Passes
CTest Fails
DTest Skipped
💡 Hint
Check the 'Test Outcome' column at step 3 in the execution_table.
At which step does the snapshot first exist?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Snapshot Exists?' column in the execution_table.
If the component output changes but you do not update the snapshot, what happens?
ATest fails due to mismatch
BTest passes because output is ignored
CSnapshot is automatically updated
DTest is skipped
💡 Hint
Refer to execution_table row 3 where output changes but snapshot mismatch causes failure.
Concept Snapshot
Snapshot Testing in Vue:
- Mount component with @vue/test-utils
- Render component HTML
- Compare HTML to saved snapshot file
- If no snapshot, create one and pass test
- If mismatch, test fails until snapshot updated
- Helps catch unexpected UI changes
Full Transcript
Snapshot testing in Vue involves mounting a component and capturing its rendered HTML output. This output is saved as a snapshot file the first time the test runs. On subsequent runs, the test compares the current output to the saved snapshot. If they match, the test passes, confirming the UI has not changed unexpectedly. If they differ, the test fails, signaling a change in the component's output. Developers can then update the snapshot if the change is intentional or fix the component if it is a bug. This process helps maintain UI consistency and catch unintended changes early.