Vue.js Testing
Automated tests check that your code behaves as expected every time you change it. A test clicks a button, reads the result, and reports pass or fail within seconds. Without tests, each edit risks breaking a feature nobody remembers to check by hand. With tests, you change code with confidence and find mistakes before visitors do.
Think of a quality inspector on a factory line. Every product passes the inspector before it ships. Tests inspect every component before it reaches your visitors.
The Testing Tools
Two tools cover most Vue projects. Vitest runs the tests and reports results, and it works directly with Vite projects. Vue Test Utils mounts components in a test environment and lets you click, type, and read output.
npm install -D vitest @vue/test-utils jsdomAdd the test environment to the Vite configuration. The jsdom package simulates a browser inside Node.
// vite.config.js
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
export default defineConfig({
plugins: [vue()],
test: {
environment: "jsdom"
}
});Add a script to package.json and run it with npm test.
"scripts": {
"test": "vitest"
}Write the First Test
A counter component gives a simple target.
<!-- Counter.vue -->
<script setup>
import { ref } from "vue";
const count = ref(0);
</script>
<template>
<p data-test="value">{{ count }}</p>
<button data-test="add" @click="count++">Add</button>
</template>
// Counter.test.js
import { describe, it, expect } from "vitest";
import { mount } from "@vue/test-utils";
import Counter from "./Counter.vue";
describe("Counter", () => {
it("starts at zero", () => {
const wrapper = mount(Counter);
expect(wrapper.get("[data-test=value]").text()).toBe("0");
});
it("adds one after a click", async () => {
const wrapper = mount(Counter);
await wrapper.get("[data-test=add]").trigger("click");
expect(wrapper.get("[data-test=value]").text()).toBe("1");
});
});The mount function builds the component. The trigger method simulates a click, and await gives Vue time to redraw before the test reads the result.
Diagram: Anatomy of a Test
Arrange --> mount(Counter)
Act --> trigger("click") on the button
Assert --> expect(text).toBe("1")Test Props
Pass props through the mount options and check what appears.
it("shows the greeting", () => {
const wrapper = mount(Greeting, { props: { name: "Maya" } });
expect(wrapper.text()).toContain("Hello, Maya");
});Test Emitted Events
A component that sends events to a parent can be checked through emitted().
it("emits a saved event", async () => {
const wrapper = mount(EditForm);
await wrapper.get("form").trigger("submit.prevent");
expect(wrapper.emitted()).toHaveProperty("saved");
});Diagram: Event Check
Test submits the form
|
Component emits "saved"
|
wrapper.emitted() = { saved: [[...]] } --> assertion passesTest Async Data
A fake replaces the real network call so the test stays fast and predictable. The vi.fn helper builds the fake, and flushPromises waits for pending work.
import { vi } from "vitest";
import { flushPromises } from "@vue/test-utils";
it("lists users", async () => {
global.fetch = vi.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve([{ id: 1, name: "Ana" }])
})
);
const wrapper = mount(UserList);
await flushPromises();
expect(wrapper.text()).toContain("Ana");
});What to Test
- Test what a visitor sees and does: text, clicks, and form entries.
- Avoid testing private details such as internal variable names, since renaming them breaks tests without breaking features.
- Use
data-testattributes for selectors that stay stable when styles change. - Cover the risky parts first: payment forms, login flows, and calculations.
Test Types at a Glance
Diagram: Testing Pyramid
/\ End-to-end tests (few, slow, full browser)
/ \
/----\ Component tests (some, medium speed)
/ \
/--------\ Unit tests (many, very fast)Unit tests check small functions, and component tests check single components. End-to-end tests with tools such as Playwright or Cypress click through the entire application in a real browser.
Summary
Vitest and Vue Test Utils form the standard Vue testing setup. mount builds a component, trigger simulates actions, emitted reads events, and fakes replace network calls. Tests focus on visible behavior, use stable data-test selectors, and run in seconds. A small suite on critical features catches mistakes before release.
