Vuetify:UnitTesting
lastmod 2026-11-10created 2021-07-274 edits
이 문서는 Vuetify 컴포넌트를 사용하는 Vue 컴포넌트의 단위 테스트 방법을 정리한다. Vue Test Utils의 mount에 Vuetify 인스턴스를 주입하여 테스트 환경을 만들고 Jest의 스냅샷 테스트나 DOM 조회로 렌더링 결과를 검증한다. 버튼 클릭 같은 사용자 동작을 시뮬레이션해 커스텀 이벤트가 발생하는지 확인하는 패턴도 다룬다.
Spec Tests
// test/CustomCard.spec.js
// Libraries
import Vue from 'vue'
import Vuetify from 'vuetify'
// Components
import CustomCard from '@/components/CustomCard'
// Utilities
import { createLocalVue, mount } from '@vue/test-utils'
describe('CustomCard.vue', () => {
const localVue = createLocalVue()
let vuetify
beforeEach(() => {
vuetify = new Vuetify()
})
it('should have a custom title and match snapshot', () => {
const wrapper = mount(CustomCard, {
localVue,
vuetify,
propsData: { title: 'Foobar' },
})
// With jest we can create snapshot files of the HTML output
expect(wrapper.html()).toMatchSnapshot()
// We could also verify this differently
// by checking the text content
const title = wrapper.find('.v-card__title > span')
expect(title.text()).toBe('Foobar')
})
it('should emit an event when the action v-btn is clicked', () => {
const wrapper = mount(CustomCard, {
localVue,
vuetify,
propsData: { title: 'Foobar' },
})
const event = jest.fn()
const button = wrapper.find('.v-btn')
// Here we bind a listener to the wrapper
// instance to catch our custom event
// https://vuejs.org/v2/api/#Instance-Methods-Events
wrapper.vm.$on('action-btn:clicked', event)
expect(event).toHaveBeenCalledTimes(0)
// Simulate a click on the button
button.trigger('click')
// Ensure that our mock event was called
expect(event).toHaveBeenCalledTimes(1)
})
})