Skip to main content

Lesson 2 of 8

Vitest Setup & Configuration

Vitest is the modern Jest replacement - built for Vite projects, sharing its config file, running tests in browsers or Node, and supporting ESM natively. For Next.js projects, Jest with SWC is also popular.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Vitest Complete Setup

Vitest Complete Setup
# Install test dependencies
npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/user-event @testing-library/jest-dom msw

# vite.config.ts - add test config (reuses Vite config)
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,              // access describe, it, expect without imports
    environment: 'jsdom',       // simulate browser DOM
    setupFiles: ['./src/test/setup.ts'],
    css: true,                  // CSS parsing (useful if code checks styles)
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
      exclude: ['node_modules/', 'src/test/', '**/*.stories.*', '**/*.d.ts'],
      thresholds: { branches: 70, functions: 70, lines: 70, statements: 70 },
    },
  },
});

// src/test/setup.ts - global test setup
import '@testing-library/jest-dom'; // adds toBeInTheDocument, toHaveValue etc.
import { server } from './mocks/server';
import { cleanup } from '@testing-library/react';

// Start MSW server before all tests
beforeAll(() => server.listen({ onUnhandledRequest: 'warn' }));
// Reset handlers after each test (removes test-specific overrides)
afterEach(() => {
  server.resetHandlers();
  cleanup(); // unmount components (RTL does this automatically, but explicit is cleaner)
});
// Stop server after all tests
afterAll(() => server.close());

// package.json scripts
{
  "scripts": {
    "test": "vitest",
    "test:run": "vitest run",          // single run (for CI)
    "test:ui": "vitest --ui",          // browser-based test UI
    "test:coverage": "vitest run --coverage"
  }
}

Tip

Tip

Practice Vitest Setup Configuration in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and flow for Vitest Setup & Configuration

Practice Task

Note

Practice Task - (1) Write a working example of Vitest Setup Configuration from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Vitest Setup Configuration is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done