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>
mount renders the component and all its child components fully, showing the complete DOM tree. shallowMount renders the component but replaces child components with simple placeholders (stubs) to isolate the parent component's behavior.
import { shallowMount } from '@vue/test-utils'; import ParentComponent from './ParentComponent.vue'; // ChildComponent has a mounted() hook that sets a data property.
shallowMount replaces child components with stubs, so their lifecycle hooks like mounted() do not run. This helps isolate the parent component during testing.
import { shallowMount } from '@vue/test-utils'; import ParentComponent from './ParentComponent.vue'; const wrapper = shallowMount(ParentComponent); expect(wrapper.text()).toContain('Child Content');
shallowMount replaces child components with stubs, so their internal template content like 'Child Content' does not appear in the rendered output. The test fails because it expects that text.
import { shallowMount } from '@vue/test-utils'; import ParentComponent from './ParentComponent.vue';
In Vue Test Utils v3+, stubs are passed inside the global option as stubs. Setting ChildComponent: true creates a stub for it.
shallowMount helps keep tests focused on the component under test by stubbing child components. This reduces test runtime and avoids failures caused by unrelated child components.