Bird
Raised Fist0
Vueframework~8 mins

Mounting components (mount vs shallowMount) in Vue - Performance Comparison

Choose your learning style10 modes available
Performance: Mounting components (mount vs shallow)
MEDIUM IMPACT
This affects the initial rendering speed and memory usage during component testing by controlling how deeply components are rendered.
Testing a Vue component with many child components
Vue
import { shallowMount } from '@vue/test-utils';
import ParentComponent from './ParentComponent.vue';

const wrapper = shallowMount(ParentComponent);
shallow renders only the parent component and stubs child components, reducing rendering and memory load.
📈 Performance Gainreduces rendering time by 70-90%; lowers memory usage; faster test execution
Testing a Vue component with many child components
Vue
import { mount } from '@vue/test-utils';
import ParentComponent from './ParentComponent.vue';

const wrapper = mount(ParentComponent);
mount renders the full component tree including all child components, increasing rendering time and memory use.
📉 Performance Costblocks rendering for 50-200ms depending on child complexity; increases memory usage significantly
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
mountFull DOM tree including childrenMultiple reflows proportional to child countHigh paint cost due to complex tree[X] Bad
shallowOnly parent component DOM nodesSingle reflowLow paint cost[OK] Good
Rendering Pipeline
mount triggers full component rendering including child components, causing more style calculations, layout, and paint steps. shallow skips child components, reducing these steps.
→Style Calculation
→Layout
→Paint
⚠️ BottleneckLayout and Paint stages due to deeper DOM tree from full mount
Core Web Vital Affected
LCP
This affects the initial rendering speed and memory usage during component testing by controlling how deeply components are rendered.
Optimization Tips
1Use shallow to speed up tests by avoiding full child component rendering.
2Full mount increases DOM nodes and triggers more layout and paint operations.
3Shallow mounting improves LCP by reducing rendering complexity during tests.
Performance Quiz - 3 Questions
Test your performance knowledge
Which mounting method renders child components fully, increasing test rendering time?
Ashallow
Bmount
Crender
Dattach
DevTools: Performance
How to check: Record a performance profile while running tests with mount and shallow. Compare flame charts for rendering time and layout events.
What to look for: Look for longer scripting and rendering times with mount. Fewer layout and paint events indicate better performance with shallow.

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