Performance: Mounting components (mount vs shallow)
This affects the initial rendering speed and memory usage during component testing by controlling how deeply components are rendered.
import { shallowMount } from '@vue/test-utils'; import ParentComponent from './ParentComponent.vue'; const wrapper = shallowMount(ParentComponent);
import { mount } from '@vue/test-utils'; import ParentComponent from './ParentComponent.vue'; const wrapper = mount(ParentComponent);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| mount | Full DOM tree including children | Multiple reflows proportional to child count | High paint cost due to complex tree | [X] Bad |
| shallow | Only parent component DOM nodes | Single reflow | Low paint cost | [OK] Good |
mount and shallowMount in Vue testing?mount behaviormount renders the component and all its children fully, meaning the entire component tree is rendered.shallowMount behaviorshallowMount renders the component but replaces child components with placeholders, isolating the component under test.mount renders full tree; shallowMount replaces children with placeholders -> Option CshallowMount from Vue Test Utils?shallowMount is from '@vue/test-utils' using named import syntax: import { shallowMount } from '@vue/test-utils';const wrapper = shallowMount(MyComponent); which matches import { shallowMount } from '@vue/test-utils'; const wrapper = shallowMount(MyComponent);.wrapper.html() include?
import { shallowMount } from '@vue/test-utils';
import Parent from './Parent.vue';
const wrapper = shallowMount(Parent);
console.log(wrapper.html());
Assuming Parent has a child component Child used in its template.shallowMount renders the component but replaces child components with stubs (placeholders).wrapper.html()<child-stub> tags instead of full Child component HTML.TypeError: mount is not a function. What is the likely cause?
import { shallowMount } from '@vue/test-utils';
const wrapper = mount(MyComponent);shallowMount but tries to call mount, which is not imported.mount is undefined, calling it causes TypeError: mount is not a function.mount from '@vue/test-utils' -> Option DshallowMount replaces child components with stubs, so child components' API calls won't run, isolating the test.mount renders full tree, causing API calls unless mocked manually, which is more complex.Pass the test and this pattern moves unknown