๐ŸŽฏ 4.2 shallowMount / mount ใฎ้•ใ„ใจไฝฟใ„ๅˆ†ใ‘


โœ… ใ€่ฆ็‚นใ€‘

โ†’ ใ€Œ่‡ชๅˆ†ใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ ใ‘ใ‚’ใƒ†ใ‚นใƒˆใ—ใŸใ„ใ€ใชใ‚‰ shallowMount

โ†’ ใ€Œๅญใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฎๅ‹•ไฝœใ‚‚ๅซใ‚ใฆใƒ†ใ‚นใƒˆใ—ใŸใ„ใ€ใชใ‚‰ mount


๐Ÿ—๏ธ ใ€ๅŸบๆœฌใฎไฝฟใ„ๆ–นใ€‘

// ไฝฟใ†ใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใ‚’import
import { shallowMount, mount } from '@vue/test-utils';
import ParentComponent from '@/components/ParentComponent.vue';

// shallowMount โ†’ ๅญใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฏใ‚นใ‚ฟใƒ–๏ผˆๅฝ็‰ฉ๏ผ‰
const wrapper = shallowMount(ParentComponent);

// mount โ†’ ๅญใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ‚‚ๆœฌ็‰ฉใงใƒžใ‚ฆใƒณใƒˆ
const wrapper = mount(ParentComponent);


๐ŸŽ‰ ใ€ๅ…ทไฝ“ไพ‹ใ€‘

ไพ‹: ่ฆชใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใจๅญใ‚ณใƒณใƒใƒผใƒใƒณใƒˆ

<!-- src/components/ChildComponent.vue -->
<template>
  <p>I'm a child</p>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';

@Component
export default class ChildComponent extends Vue {}
</script>

<!-- src/components/ParentComponent.vue -->
<template>
  <div>
    <h1>Parent</h1>
    <ChildComponent />
  </div>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';
import ChildComponent from './ChildComponent.vue';

@Component({
  components: { ChildComponent },
})
export default class ParentComponent extends Vue {}
</script>


๐ŸŸข ใ€shallowMount ใฎๅ‹•ไฝœใ€‘

import { shallowMount } from '@vue/test-utils';
import ParentComponent from '@/components/ParentComponent.vue';

test('ParentComponent shallowMount', () => {
  const wrapper = shallowMount(ParentComponent);
  // ๅญใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฏ <child-component-stub> ใซใชใ‚‹
  expect(wrapper.html()).toContain('<childcomponent-stub></childcomponent-stub>');
});

๐Ÿ“ ๅ‡บๅŠ›๏ผš