Challenge - 5 Problems
Snapshot Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Vue snapshot test verify?
Consider this Vue component snapshot test. What aspect of the component does it check?
Vue
import { mount } from '@vue/test-utils'; import MyButton from '@/components/MyButton.vue'; test('MyButton matches snapshot', () => { const wrapper = mount(MyButton, { props: { label: 'Click me' } }); expect(wrapper.html()).toMatchSnapshot(); });
Attempts:
2 left
💡 Hint
Snapshot tests compare the rendered output to a saved version.
✗ Incorrect
Snapshot testing captures the HTML output of a component and compares it to a saved snapshot file. This helps detect unexpected changes in the UI.
📝 Syntax
intermediate2:00remaining
Which option correctly updates a Vue snapshot test after intentional UI changes?
You updated the MyButton component's template. How do you update the snapshot test to accept the new output?
Vue
import { mount } from '@vue/test-utils'; import MyButton from '@/components/MyButton.vue'; test('MyButton matches snapshot', () => { const wrapper = mount(MyButton, { props: { label: 'Press me' } }); expect(wrapper.html()).toMatchSnapshot(); });
Attempts:
2 left
💡 Hint
Testing tools often have a flag to update snapshots automatically.
✗ Incorrect
Using the --updateSnapshot flag tells the test runner to replace the old snapshot with the new output, keeping tests in sync with intentional changes.
🔧 Debug
advanced2:00remaining
Why does this Vue snapshot test fail unexpectedly?
This snapshot test fails even though the component code did not change. What is the likely cause?
Vue
import { mount } from '@vue/test-utils'; import ClockDisplay from '@/components/ClockDisplay.vue'; test('ClockDisplay matches snapshot', () => { const wrapper = mount(ClockDisplay); expect(wrapper.html()).toMatchSnapshot(); }); // ClockDisplay shows current time dynamically.
Attempts:
2 left
💡 Hint
Snapshot tests expect consistent output, but dynamic data can cause mismatches.
✗ Incorrect
If a component renders changing data like the current time, the snapshot will differ each run, causing failures. Mocking or freezing time is needed.
❓ state_output
advanced2:00remaining
What is the snapshot test output after this state change?
Given this Vue component and test, what will the snapshot contain after clicking the button?
Vue
import { mount } from '@vue/test-utils'; import { ref } from 'vue'; const Counter = { template: `<button @click="count++">Count: {{ count }}</button>`, setup() { const count = ref(0); return { count }; } }; test('Counter snapshot after click', async () => { const wrapper = mount(Counter); await wrapper.trigger('click'); expect(wrapper.html()).toMatchSnapshot(); });
Attempts:
2 left
💡 Hint
Clicking the button increases the count by 1 before snapshot.
✗ Incorrect
The button starts at 0. After the click event, count increments to 1. The snapshot captures the updated HTML showing 'Count: 1'.
🧠 Conceptual
expert2:00remaining
Why should snapshot tests be used carefully with Vue components?
Which reason best explains a limitation of snapshot testing Vue components?
Attempts:
2 left
💡 Hint
Think about how small UI changes affect snapshot tests.
✗ Incorrect
Snapshot tests are sensitive to any change in rendered output, including formatting or style tweaks, which can cause many test failures even if functionality is fine.