Performance: Snapshot testing
Snapshot testing affects development and test run speed but does not impact page load or rendering performance in the browser.
import { mount } from '@vue/test-utils'; import MyComponent from './MyComponent.vue'; test('matches snapshot', () => { const wrapper = mount(MyComponent); expect(wrapper.html()).toMatchSnapshot(); });
import { mount } from '@vue/test-utils'; import MyComponent from './MyComponent.vue'; test('renders correctly', () => { const wrapper = mount(MyComponent); expect(wrapper.html()).toBe('<div>Expected HTML here</div>'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Hardcoded HTML string comparison | 0 (test only) | 0 | 0 | [X] Bad for test maintenance speed |
| Snapshot testing with stored output | 0 (test only) | 0 | 0 | [OK] Good for test speed and maintenance |
mount from @vue/test-utils and calling html() returns the rendered HTML string.toMatchSnapshot() is the correct Jest matcher to compare the HTML output to the saved snapshot.import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'
test('renders correctly', () => {
const wrapper = mount(MyComponent)
expect(wrapper.html()).toMatchSnapshot()
})expect(wrapper.html().toMatchSnapshot())What is the problem and how to fix it?
toMatchSnapshot() on the string result of wrapper.html(), which is incorrect.toMatchSnapshot() must be called on the expect() wrapper, not on the string itself.Pass the test and this pattern moves unknown