Discover how to speed up your Vue tests by controlling what parts of your components actually render!
Mounting components (mount vs shallowMount) in Vue - When to Use Which
Imagine testing a Vue component that includes many child components. You try to test it by rendering everything fully, but it takes a long time and is hard to control.
Manually rendering all child components makes tests slow and complex. It's easy to get lost in details and hard to isolate the part you want to test.
Using mount and shallowMount lets you choose how deep to render components. shallowMount renders only the component itself, skipping children, making tests faster and simpler.
const wrapper = mount(MyComponent) // renders all children fully
const wrapper = shallowMount(MyComponent) // renders only MyComponent, stubs children
This lets you write focused, fast tests by controlling how much of the component tree you render.
Testing a form component with many input fields and buttons: using shallowMount tests just the form logic without rendering every input's internal details.
Mounting components fully renders all children, which can slow tests.
Shallow mounting renders only the component itself, skipping children.
This helps write faster, clearer, and more focused tests.
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
