0
0
Vueframework~20 mins

Mounting components (mount vs shallowMount) in Vue - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Mount Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Difference in rendered output between mount and shallowMount
Given a Vue component that includes a child component, what will be the difference in the rendered output when using mount versus shallowMount?
Vue
import { mount, shallowMount } from '@vue/test-utils';
import ParentComponent from './ParentComponent.vue';

// ParentComponent template:
// <template>
//   <div>
//     <ChildComponent />
//   </div>
// </template>

// ChildComponent template:
// <template>
//   <span>Child Content</span>
// </template>
Amount renders only ParentComponent without ChildComponent; shallowMount renders both ParentComponent and ChildComponent fully.
Bmount replaces ChildComponent with a stub; shallowMount fully renders ChildComponent inside ParentComponent.
Cmount and shallowMount both fully render ParentComponent and ChildComponent with no difference.
Dmount renders ParentComponent and fully renders ChildComponent inside it; shallowMount renders ParentComponent but replaces ChildComponent with a stub.
Attempts:
2 left
💡 Hint
Think about how shallowMount avoids rendering child components fully to isolate the parent.
state_output
intermediate
2:00remaining
Effect of shallowMount on child component lifecycle hooks
When using shallowMount on a Vue component with child components, which lifecycle hooks of the child components are called?
Vue
import { shallowMount } from '@vue/test-utils';
import ParentComponent from './ParentComponent.vue';

// ChildComponent has a mounted() hook that sets a data property.
AChild components lifecycle hooks are called normally as if fully mounted.
BChild components lifecycle hooks like mounted() are not called because they are stubbed.
COnly created() hooks of child components are called, but not mounted().
DChild components lifecycle hooks are called twice due to shallowMount behavior.
Attempts:
2 left
💡 Hint
Consider what shallowMount does to child components in the render tree.
🔧 Debug
advanced
2:00remaining
Why does a test fail when using shallowMount instead of mount?
A test expects a child component's rendered text to be present, but it fails when using shallowMount. Why?
Vue
import { shallowMount } from '@vue/test-utils';
import ParentComponent from './ParentComponent.vue';

const wrapper = shallowMount(ParentComponent);
expect(wrapper.text()).toContain('Child Content');
ABecause shallowMount replaces child components with stubs, the child's text is not rendered in the output.
BBecause shallowMount does not mount the parent component at all, so no text is rendered.
CBecause shallowMount mounts child components twice, confusing the test.
DBecause shallowMount causes a syntax error preventing rendering.
Attempts:
2 left
💡 Hint
Think about what shallowMount does to child components in the DOM.
📝 Syntax
advanced
2:00remaining
Correct usage of mount and shallowMount with options
Which option correctly mounts a Vue component with a stub for a child component named 'ChildComponent' using shallowMount?
Vue
import { shallowMount } from '@vue/test-utils';
import ParentComponent from './ParentComponent.vue';
AshallowMount(ParentComponent, { global: { stubs: { ChildComponent: true } } })
BshallowMount(ParentComponent, { stubs: ['ChildComponent'] })
CshallowMount(ParentComponent, { stubs: { ChildComponent: false } })
DshallowMount(ParentComponent, { global: { components: { ChildComponent: true } } })
Attempts:
2 left
💡 Hint
Check the correct property name and structure for stubbing in Vue Test Utils v3+.
🧠 Conceptual
expert
2:00remaining
Why choose shallowMount over mount in large component trees?
In a large Vue application with deeply nested components, why might a developer prefer using shallowMount instead of mount for unit tests?
AshallowMount renders all child components fully, providing more complete integration testing.
BshallowMount automatically fixes bugs in child components during tests.
CshallowMount improves test speed and isolates the component by not rendering child components fully, reducing complexity.
DshallowMount disables all lifecycle hooks in the parent component to speed up tests.
Attempts:
2 left
💡 Hint
Think about test isolation and performance when testing components with many children.