Mounting components lets you test how Vue components behave. mount renders the full component with all children, while shallowMount renders only the component itself, skipping child components.
Mounting components (mount vs shallowMount) in Vue
import { mount, shallowMount } from '@vue/test-utils' const wrapper = mount(Component) const shallowWrapper = shallowMount(Component)
mount renders the component and all its child components fully.
shallowMount renders the component but replaces child components with placeholders.
MyComponent fully, including all child components.import { mount } from '@vue/test-utils' import MyComponent from './MyComponent.vue' const wrapper = mount(MyComponent)
MyComponent but replaces child components with stubs.import { shallowMount } from '@vue/test-utils' import MyComponent from './MyComponent.vue' const wrapper = shallowMount(MyComponent)
This example shows the difference between mount and shallowMount. The full mount renders the Child component inside Parent. The shallow mount replaces Child with a stub, so you only see the placeholder.
import { mount, shallowMount } from '@vue/test-utils' import { defineComponent } from 'vue' // Child component const Child = defineComponent({ name: 'Child', template: '<div>Child content</div>' }) // Parent component uses Child const Parent = defineComponent({ name: 'Parent', components: { Child }, template: '<div>Parent content <Child /></div>' }) // Full mount const fullWrapper = mount(Parent) console.log('Full mount HTML:', fullWrapper.html()) // Shallow mount const shallowWrapper = shallowMount(Parent) console.log('Shallow mount HTML:', shallowWrapper.html())
Use shallowMount to isolate the component you want to test and avoid testing child components.
Use mount when you want to test the full component tree and interactions between components.
Shallow mounting can speed up tests and reduce complexity.
mount renders the component and all its children fully.
shallowMount renders the component but replaces children with placeholders.
Choose based on whether you want to test the full component tree or isolate the component.
Practice
mount and shallowMount in Vue testing?Solution
Step 1: Understand
mountbehaviormountrenders the component and all its children fully, meaning the entire component tree is rendered.Step 2: Understand
shallowMountbehaviorshallowMountrenders the component but replaces child components with placeholders, isolating the component under test.Final Answer:
mountrenders full tree;shallowMountreplaces children with placeholders -> Option CQuick Check:
mount = full render, shallowMount = shallow render [OK]
- Confusing which method renders children fully
- Thinking shallowMount runs component logic without children
- Assuming mount skips child components
shallowMount from Vue Test Utils?Solution
Step 1: Check import statement
The correct import forshallowMountis from '@vue/test-utils' using named import syntax:import { shallowMount } from '@vue/test-utils';Step 2: Check usage syntax
Usage isconst wrapper = shallowMount(MyComponent);which matches import { shallowMount } from '@vue/test-utils'; const wrapper = shallowMount(MyComponent);.Final Answer:
import { shallowMount } from '@vue/test-utils'; const wrapper = shallowMount(MyComponent); -> Option BQuick Check:
Correct import and usage = import { shallowMount } from '@vue/test-utils'; const wrapper = shallowMount(MyComponent); [OK]
- Using default import instead of named import
- Incorrect import path or syntax
- Calling shallowMount without importing
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.Solution
Step 1: Understand shallowMount behavior
shallowMountrenders the component but replaces child components with stubs (placeholders).Step 2: Analyze output of
The HTML will include Parent's template withwrapper.html()<child-stub>tags instead of full Child component HTML.Final Answer:
Parent HTML with child components replaced by <child-stub> placeholders -> Option AQuick Check:
shallowMount = stubs children [OK]
- Expecting full child component HTML with shallowMount
- Thinking shallowMount returns empty HTML
- Confusing mount and shallowMount output
TypeError: mount is not a function. What is the likely cause?
import { shallowMount } from '@vue/test-utils';
const wrapper = mount(MyComponent);Solution
Step 1: Check imports
The code imports onlyshallowMountbut tries to callmount, which is not imported.Step 2: Understand error cause
Sincemountis undefined, calling it causesTypeError: mount is not a function.Final Answer:
Forgot to importmountfrom '@vue/test-utils' -> Option DQuick Check:
Import mount to use mount() [OK]
- Assuming shallowMount can be used as mount
- Not importing mount at all
- Misunderstanding error message
Solution
Step 1: Identify test goal
You want to isolate the parent component and avoid network requests from child components.Step 2: Choose mounting method
shallowMountreplaces child components with stubs, so child components' API calls won't run, isolating the test.Step 3: Compare alternatives
mountrenders full tree, causing API calls unless mocked manually, which is more complex.Final Answer:
Use shallowMount to isolate parent and avoid child API calls -> Option AQuick Check:
shallowMount isolates component, avoids child side effects [OK]
- Using mount and forgetting to mock child API calls
- Thinking mount automatically isolates children
- Ignoring child component effects in tests
Ready to prove it?
Pass the test and this pattern moves unknown
