Bird
Raised Fist0
Vueframework~5 mins

Mounting components (mount vs shallowMount) in Vue

Choose your learning style10 modes available
Introduction

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.

You want to test a component including its child components to see full behavior.
You want to test a component alone without running child components to isolate issues.
You want faster tests by skipping rendering of child components.
You want to check if a component renders correctly without worrying about children.
You want to mock child components to focus on the parent component's logic.
Syntax
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.

Examples
This mounts MyComponent fully, including all child components.
Vue
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'

const wrapper = mount(MyComponent)
This mounts MyComponent but replaces child components with stubs.
Vue
import { shallowMount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'

const wrapper = shallowMount(MyComponent)
Sample Program

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.

Vue
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())
OutputSuccess
Important Notes

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.

Summary

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

(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

  1. Step 1: Understand mount behavior

    mount renders the component and all its children fully, meaning the entire component tree is rendered.
  2. Step 2: Understand shallowMount behavior

    shallowMount renders the component but replaces child components with placeholders, isolating the component under test.
  3. Final Answer:

    mount renders full tree; shallowMount replaces children with placeholders -> Option C
  4. 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

  1. 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';
  2. Step 2: Check usage syntax

    Usage is const wrapper = shallowMount(MyComponent); which matches import { shallowMount } from '@vue/test-utils'; const wrapper = shallowMount(MyComponent);.
  3. Final Answer:

    import { shallowMount } from '@vue/test-utils'; const wrapper = shallowMount(MyComponent); -> Option B
  4. 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

  1. Step 1: Understand shallowMount behavior

    shallowMount renders the component but replaces child components with stubs (placeholders).
  2. Step 2: Analyze output of wrapper.html()

    The HTML will include Parent's template with <child-stub> tags instead of full Child component HTML.
  3. Final Answer:

    Parent HTML with child components replaced by <child-stub> placeholders -> Option A
  4. 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

  1. Step 1: Check imports

    The code imports only shallowMount but tries to call mount, which is not imported.
  2. Step 2: Understand error cause

    Since mount is undefined, calling it causes TypeError: mount is not a function.
  3. Final Answer:

    Forgot to import mount from '@vue/test-utils' -> Option D
  4. 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

  1. Step 1: Identify test goal

    You want to isolate the parent component and avoid network requests from child components.
  2. Step 2: Choose mounting method

    shallowMount replaces child components with stubs, so child components' API calls won't run, isolating the test.
  3. Step 3: Compare alternatives

    mount renders full tree, causing API calls unless mocked manually, which is more complex.
  4. Final Answer:

    Use shallowMount to isolate parent and avoid child API calls -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard