0
0
Vueframework~20 mins

Snapshot testing in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Snapshot Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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();
});
AIt checks that the rendered HTML output of MyButton stays the same over time.
BIt verifies that the MyButton component emits a click event when clicked.
CIt tests that the MyButton component updates its label prop correctly.
DIt ensures that the MyButton component's methods are called during rendering.
Attempts:
2 left
💡 Hint
Snapshot tests compare the rendered output to a saved version.
📝 Syntax
intermediate
2: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();
});
ARun the test with the --updateSnapshot flag to refresh the stored snapshot.
BManually edit the snapshot file to match the new output HTML.
CDelete the snapshot file and rerun the test normally.
DChange expect(wrapper.html()).toEqual(newHtmlString) with the new HTML.
Attempts:
2 left
💡 Hint
Testing tools often have a flag to update snapshots automatically.
🔧 Debug
advanced
2: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.
AThe test is missing an await for asynchronous rendering.
BThe snapshot file is corrupted and cannot be read.
CThe mount function is missing required props causing render errors.
DThe component renders dynamic content (current time) that changes every test run.
Attempts:
2 left
💡 Hint
Snapshot tests expect consistent output, but dynamic data can cause mismatches.
state_output
advanced
2: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();
});
A<button>Count: 0</button>
B<button>Count: 1</button>
C<button>Count: </button>
D<button>Count: undefined</button>
Attempts:
2 left
💡 Hint
Clicking the button increases the count by 1 before snapshot.
🧠 Conceptual
expert
2:00remaining
Why should snapshot tests be used carefully with Vue components?
Which reason best explains a limitation of snapshot testing Vue components?
ASnapshots do not check if component methods are called correctly during events.
BSnapshots cannot be used with Vue components that use the Composition API.
CSnapshots can break easily with minor style or markup changes, causing noisy test failures.
DSnapshots require manual updates for every prop change, making tests slow to write.
Attempts:
2 left
💡 Hint
Think about how small UI changes affect snapshot tests.