vitest

Use when writing or configuring Vitest tests - assertions, mocking, coverage, and workspace-aware testing for TypeScript projects

⭐5stars🔀1forks📁View on GitHub🕐Updated Jan 11, 2026

When & Why to Use This Skill

This Claude skill provides comprehensive support for writing, configuring, and optimizing Vitest tests within TypeScript projects. It streamlines the development lifecycle by assisting with core assertions, advanced mocking techniques, code coverage analysis, and workspace-aware testing configurations, ensuring robust and high-quality codebases.

Use Cases

  • Unit Testing: Rapidly generating test suites using core assertions such as deep equality, partial object matching, and snapshot testing to verify component logic.
  • Mocking & Spying: Implementing complex mocking strategies for modules and dependencies using the `vi` utility to isolate units of code and verify function calls.
  • Environment Configuration: Setting up and fine-tuning `vitest.config.ts` for various environments, including Node.js, browser-like globals, and coverage provider integration.
  • Monorepo Management: Configuring workspace-aware testing for large-scale TypeScript projects to run tests efficiently across multiple packages.
  • Async Testing: Developing reliable test cases for asynchronous operations, ensuring proper handling of promises and timed-out executions.
namevitest
descriptionUse when writing or configuring Vitest tests - assertions, mocking, coverage, and workspace-aware testing for TypeScript projects

Vitest Testing Framework

Quick Start

import { describe, it, expect, vi, beforeEach } from 'vitest';

describe('Calculator', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('adds numbers correctly', () => {
    expect(add(2, 3)).toBe(5);
  });

  it('handles async operations', async () => {
    const result = await fetchData();
    expect(result).toMatchObject({ id: 1 });
  });

  it('mocks dependencies', () => {
    const mockFn = vi.fn().mockReturnValue(42);
    expect(mockFn()).toBe(42);
    expect(mockFn).toHaveBeenCalledOnce();
  });
});

Core Assertions

Assertion Purpose
toBe(value) Strict equality (===)
toEqual(value) Deep equality
toMatchObject(obj) Partial object match
toContain(item) Array/string contains
toThrow(error?) Function throws
toMatchSnapshot() Snapshot testing
toHaveBeenCalledWith() Mock call verification

Mocking

// Mock module
vi.mock('./api', () => ({ fetchUser: vi.fn() }));

// Spy on method
const spy = vi.spyOn(object, 'method');

// Mock implementation
mockFn.mockImplementation((x) => x * 2);
mockFn.mockResolvedValue({ data: [] });
mockFn.mockRejectedValue(new Error('fail'));

Configuration (vitest.config.ts)

import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    globals: true,
    environment: 'node',
    include: ['**/*.test.ts'],
    coverage: { provider: 'v8', reporter: ['text', 'html'] }
  }
});

CLI Commands

  • vitest - Watch mode
  • vitest run - Single run
  • vitest run --coverage - With coverage
  • vitest --workspace - Monorepo mode