Mounting Vue Components with mount vs shallowMount
📖 Scenario: You are testing a simple Vue component that shows a user's name and a button to greet them. You want to learn how to mount this component fully and shallowly to understand the difference.
🎯 Goal: Build a Vue test setup where you first mount the full component, then shallow mount it, and observe how child components behave in each case.
📋 What You'll Learn
Create a Vue component called UserGreeting with a child component GreetingMessage
Use mount from Vue Test Utils to fully mount UserGreeting
Use shallowMount from Vue Test Utils to shallow mount UserGreeting
Observe the difference in rendering between mount and shallowMount
💡 Why This Matters
🌍 Real World
Testing Vue components is essential to ensure they behave correctly before deploying apps. Understanding mount vs shallowMount helps write faster and more focused tests.
💼 Career
Vue developers and testers use these mounting methods daily to write unit tests that catch bugs early and improve code quality.
Progress0 / 4 steps
1
Create the Vue component with a child component
Create a Vue component called UserGreeting that includes a child component called GreetingMessage. The UserGreeting component should have a name prop set to 'Alice'. The GreetingMessage component should display Hello, Alice! using the name prop passed from UserGreeting.
Vue
Hint
Define two components using defineComponent. Pass the name prop from UserGreeting to GreetingMessage.
2
Import Vue Test Utils and set up mount
Import mount from @vue/test-utils. Create a variable called fullWrapper that mounts the UserGreeting component fully.
Vue
Hint
Use import { mount } from '@vue/test-utils' and then const fullWrapper = mount(UserGreeting).
3
Set up shallowMount for shallow rendering
Import shallowMount from @vue/test-utils. Create a variable called shallowWrapper that shallow mounts the UserGreeting component.
Vue
Hint
Import shallowMount alongside mount and create shallowWrapper using shallowMount(UserGreeting).
4
Add template to show both wrappers for comparison
Add a template section that shows the HTML output of both fullWrapper and shallowWrapper by binding their html() results inside pre tags with labels Full Mount Output and Shallow Mount Output.
Vue
Hint
Use <pre> tags with aria-label for accessibility and bind fullWrapper.html() and shallowWrapper.html() inside them.
Practice
(1/5)
1. What is the main difference between mount and shallowMount in Vue testing?
easy
A. mount is used only for functional components, while shallowMount is for stateful components.
B. mount only renders the component's template without any logic, while shallowMount renders the full component with logic.
C. mount renders the component and all its child components fully, while shallowMount renders the component but replaces child components with placeholders.
D. mount skips rendering child components, while shallowMount renders all children fully.
Solution
Step 1: Understand mount behavior
mount renders the component and all its children fully, meaning the entire component tree is rendered.
Step 2: Understand shallowMount behavior
shallowMount renders the component but replaces child components with placeholders, isolating the component under test.
Final Answer:
mount renders full tree; shallowMount replaces children with placeholders -> Option C
Quick Check:
mount = full render, shallowMount = shallow render [OK]
Hint: mount = full render, shallowMount = isolate children [OK]
Common Mistakes:
Confusing which method renders children fully
Thinking shallowMount runs component logic without children
Assuming mount skips child components
2. Which of the following is the correct syntax to import and use shallowMount from Vue Test Utils?
easy
A. import { mount } from '@vue/test-utils'; const wrapper = mount(MyComponent);
B. import { shallowMount } from '@vue/test-utils'; const wrapper = shallowMount(MyComponent);
C. import shallowMount from 'vue-test-utils'; const wrapper = shallowMount(MyComponent);
D. const wrapper = shallowMount(MyComponent) from '@vue/test-utils';
Solution
Step 1: Check import statement
The correct import for shallowMount is from '@vue/test-utils' using named import syntax: import { shallowMount } from '@vue/test-utils';
Step 2: Check usage syntax
Usage is const 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 B
Quick Check:
Correct import and usage = import { shallowMount } from '@vue/test-utils'; const wrapper = shallowMount(MyComponent); [OK]
Hint: Use named import { shallowMount } from '@vue/test-utils' [OK]
Common Mistakes:
Using default import instead of named import
Incorrect import path or syntax
Calling shallowMount without importing
3. Given the following Vue test code, what will 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.
medium
A. Only the HTML of Parent with <child-stub> placeholder instead of Child components.
B. Only the Child component's HTML without Parent's template.
C. An empty string because shallowMount does not render anything.
D. The full HTML of Parent and all rendered Child components.
Solution
Step 1: Understand shallowMount behavior
shallowMount renders the component but replaces child components with stubs (placeholders).
Step 2: Analyze output of wrapper.html()
The HTML will include Parent's template with <child-stub> tags instead of full Child component HTML.
Final Answer:
Parent HTML with child components replaced by <child-stub> placeholders -> Option A
Quick Check:
shallowMount = stubs children [OK]
Hint: shallowMount replaces children with stubs in HTML output [OK]
Common Mistakes:
Expecting full child component HTML with shallowMount
Thinking shallowMount returns empty HTML
Confusing mount and shallowMount output
4. You wrote this test code but get an error: TypeError: mount is not a function. What is the likely cause?
import { shallowMount } from '@vue/test-utils';
const wrapper = mount(MyComponent);
medium
A. The component MyComponent is not defined.
B. You should use shallowMount instead of mount always.
C. You need to call mount() as a method on shallowMount.
D. You forgot to import mount from '@vue/test-utils'.
Solution
Step 1: Check imports
The code imports only shallowMount but tries to call mount, which is not imported.
Step 2: Understand error cause
Since mount is undefined, calling it causes TypeError: mount is not a function.
Final Answer:
Forgot to import mount from '@vue/test-utils' -> Option D
Quick Check:
Import mount to use mount() [OK]
Hint: Import mount before using it to avoid TypeError [OK]
Common Mistakes:
Assuming shallowMount can be used as mount
Not importing mount at all
Misunderstanding error message
5. You want to test a Vue component that uses a complex child component which makes API calls. To isolate your test and avoid network requests, which mounting method should you use and why?
hard
A. Use shallowMount to replace child components with stubs, isolating the parent component and avoiding API calls.
B. Use mount to render everything fully, including child components, to test integration.
C. Use mount but mock the API calls inside child components manually.
D. Use neither; test only the child component separately.
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
shallowMount replaces child components with stubs, so child components' API calls won't run, isolating the test.
Step 3: Compare alternatives
mount renders 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 A
Quick Check:
shallowMount isolates component, avoids child side effects [OK]
Hint: Use shallowMount to isolate and avoid child side effects [OK]
Common Mistakes:
Using mount and forgetting to mock child API calls
Thinking mount automatically isolates children
Ignoring child component effects in tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.