0Pricing
Vue Academy · Lesson

Vitest Setup for Vue Projects

vitest.config.ts, jsdom environment, @vue/test-utils, coverage with v8, test script.

Vitest Setup for Vue Projects is a free Vue Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Vitest for Vue?

Vitest is a fast test runner built on Vite. Because Vue 3 projects already use Vite, Vitest reuses the same config and transforms — no separate build pipeline for tests.

It gives you a Jest-like API with native ESM and TypeScript support.

Installing the Tooling

Add Vitest, the Vue testing utilities, and jsdom (a browser-like DOM for Node).

// terminal
// npm install -D vitest @vue/test-utils jsdom @vitest/coverage-v8

The jsdom Environment

Components need a DOM to mount into. Node has no DOM, so tests run in jsdom, which simulates browser APIs. Set it as the test environment in config.

// vitest.config.ts
import { defineConfig } from "vitest/config"
import vue from "@vitejs/plugin-vue"

export default defineConfig({
  plugins: [vue()],
  test: {
    environment: "jsdom",
  },
})

Enabling Globals

By default you import describe, it, and expect in every file. Setting globals: true makes them available everywhere without imports, like Jest.

// vitest.config.ts
export default defineConfig({
  test: {
    environment: "jsdom",
    globals: true,
  },
})

TypeScript Types for Globals

When using globals with TypeScript, add Vitest types so the editor knows about describe and friends.

// tsconfig.json
{
  "compilerOptions": {
    "types": ["vitest/globals"]
  }
}

A First Test File

Vitest discovers files ending in .test.ts or .spec.ts. With globals on, you write tests directly.

// math.test.ts
describe("add", () => {
  it("sums two numbers", () => {
    expect(1 + 2).toBe(3)
  })
})

Mounting a Component

With @vue/test-utils installed and jsdom configured, you can mount real components in a test and assert on their rendered output.

import { mount } from "@vue/test-utils"
import Hello from "./Hello.vue"

it("renders the greeting", () => {
  const wrapper = mount(Hello, { props: { name: "Ada" } })
  expect(wrapper.text()).toContain("Hello, Ada")
})

Adding Coverage

Coverage shows which lines your tests exercise. Vitest supports the v8 provider (fast, native). Configure it under test.coverage.

// vitest.config.ts
export default defineConfig({
  test: {
    coverage: {
      provider: "v8",
      reporter: ["text", "html"],
    },
  },
})

The HTML Coverage Report

The html reporter writes a browsable report (default coverage/index.html). Open it to see per-file line and branch coverage with highlighted gaps.

// after running coverage:
// open coverage/index.html in a browser
// red lines = not executed by any test

package.json Scripts

Wire up convenient scripts. test runs the suite; coverage runs with the coverage flag.

// package.json
{
  "scripts": {
    "test": "vitest",
    "test:run": "vitest run",
    "coverage": "vitest run --coverage"
  }
}

Watch vs Run Mode

Plain vitest stays in watch mode, re-running affected tests on save — great while developing. In CI you want a single pass that exits, so use vitest run.

// local dev:  npm run test     (watch)
// CI pipeline: npm run test:run (one pass, exits)

Quick Check

Check your understanding of Vitest setup.

Recap

You configured Vitest for a Vue project:

  • Install vitest, @vue/test-utils, jsdom, and a coverage provider.
  • Set environment: "jsdom" so components have a DOM.
  • globals: true avoids importing test helpers; add vitest/globals to tsconfig types.
  • Configure coverage with the v8 provider and an html reporter.
  • Add test and coverage scripts; use vitest run in CI.

Frequently asked questions

Is the “Vitest Setup for Vue Projects” lesson free?

Yes — the full text of “Vitest Setup for Vue Projects” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

What will I learn in “Vitest Setup for Vue Projects”?

vitest.config.ts, jsdom environment, @vue/test-utils, coverage with v8, test script. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Vitest Setup for Vue Projects” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Vitest Setup for Vue Projects
  2. Mounting and Querying Components
  3. Testing User Interactions and Events
  4. Mocking Composables, Stores, and APIs
← Back to Vue Academy