Unit Testing
Add regression protection by adding unit tests to your Vuetify application
Usage
Unit tests are an important (and sometimes ignored) part of developing applications. They help us secure our processes and workflows, ensuring that the most critical parts of our projects are protected from accidental mistakes or oversights in our development.
Because of this, Vue has its own testing utility called vue-test-utils. It provides useful features for interacting with Vue components and works with many popular test runners.
Using Vite
Vite is a fast, opinionated frontend build tool that serves your code via native ES Module imports during dev and bundles it with Rollup for production. It provides a great developer experience and is the recommended build tool for Vuetify applications.
First, update your vite.config.js file to inline the vuetify
dependency:
import { defineConfig } from 'vite'
export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
server: {
deps: {
inline: ['vuetify'],
},
},
},
})
Setup Vitest
Vitest is a popular test runner that provides a great developer experience. It is fast, easy to use, and provides useful features like snapshot testing. To get started, install the following dependencies:
yarn add @vue/test-utils vitest resize-observer-polyfill --dev
npm install @vue/test-utils vitest resize-observer-polyfill --save-dev
pnpm add @vue/test-utils vitest resize-observer-polyfill --save-dev
bun add @vue/test-utils vitest resize-observer-polyfill --dev
Once installed, create a new folder at the root of your application named tests/spec and add a new file named HelloWorld.spec.js. The following example shows how to setup a basic unit test for a Vuetify component:
import { mount } from '@vue/test-utils'
import { expect, test } from 'vitest'
import { createVuetify } from 'vuetify'
import * as components from 'vuetify/components'
import * as directives from 'vuetify/directives'
import HelloWorld from '../../src/components/HelloWorld.vue'
const vuetify = createVuetify({
components,
directives,
})
global.ResizeObserver = require('resize-observer-polyfill')
test('displays message', () => {
const wrapper = mount({
template: '<v-layout><hello-world></hello-world></v-layout>'
}, {
props: {},
global: {
components: {
HelloWorld,
},
plugins: [vuetify],
}
})
// Assert the rendered text of the component
expect(wrapper.text()).toContain('Components')
})