agentleFS
Leo-AtienzaLeo-Atienza/atlas-claude/skills/vitest-testing/SKILL.md

vitest-testing

Skill4 starsChanged 4 months agoMITView on GitHub

Vitest — fast unit testing with Testing Library integration and test pyramid strategy

  • Installs packages

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use --pool=threads (default) for speed, --pool=forks for isolation

    line 284
  2. Very specific:

    Avoid beforeAll database seeding — use per-test factories instead

    line 287

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Vitest Testing

When to Use This Skill

Apply when writing tests for any Vite-based or modern JS/TS project. Vitest is the 2026 default (6x faster than Jest, native ESM, shared Vite config). Test pyramid: Vitest for unit + component, Playwright for E2E. Auto-activate on keywords: vitest, test, describe, it, expect, mock, coverage.

Related skills: Playwright for E2E browser testing — via the anthropic-skills:playwright-cli skill or the Claude Browser / MCP_DOCKER browser tools (the old SK-009 Playwright skill is retired).

Installation

npm install -D vitest @testing-library/react @testing-library/jest-dom @testing-library/user-event jsdom

Configuration

// vitest.config.ts (or in vite.config.ts)
import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,                  // no import { describe, it, expect }
    environment: 'jsdom',           // 'node' for server code
    setupFiles: './src/test/setup.ts',
    include: ['**/*.{test,spec}.{ts,tsx}'],
    coverage: {
      provider: 'v8',               // or 'istanbul'
      reporter: ['text', 'html', 'lcov'],
      thresholds: { lines: 80, branches: 80, functions: 80 },
    },
    css: true,                      // process CSS imports
  },
});

Setup file:

// src/test/setup.ts
import '@testing-library/jest-dom/vitest';

Basic Test Structure (Arrange-Act-Assert)

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

describe('calculateTotal', () => {
  it('returns sum of item prices', () => {
    // Arrange
    const items = [{ price: 10 }, { price: 20 }, { price: 30 }];
    // Act
    const total = calculateTotal(items);
    // Assert
    expect(total).toBe(60);
  });

  it('returns 0 for empty array', () => {
    expect(calculateTotal([])).toBe(0);
  });

  it('throws on negative prices', () => {
    expect(() => calculateTotal([{ price: -5 }])).toThrow('Invalid price');
  });
});

Component Testing with Testing Library

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, it, expect, vi } from 'vitest';
import { Counter } from './Counter';

describe('Counter', () => {
  it('increments count on button click', async () => {
    const user = userEvent.setup();
    render(<Counter initialCount={0} />);

    expect(screen.getByText('Count: 0')).toBeInTheDocument();

    await user.click(screen.getByRole('button', { name: /increment/i }));

    expect(screen.getByText('Count: 1')).toBeInTheDocument();
  });

  it('calls onChange callback with new value', async () => {
    const onChange = vi.fn();
    const user = userEvent.setup();
    render(<Counter initialCount={5} onChange={onChange} />);

    await user.click(screen.getByRole('button', { name: /increment/i }));

    expect(onChange).toHaveBeenCalledWith(6);
  });
});

Mocking

Module mocks:

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

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

describe('UserService', () => {
  it('fetches and transforms user', async () => {
    vi.mocked(fetchUser).mockResolvedValue({ id: '1', name: 'Alice' });
    const result = await getUser('1');
    expect(result.displayName).toBe('Alice');
  });
});

Spy on methods:

const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
// ... test code that should log error ...
expect(spy).toHaveBeenCalledWith(expect.stringContaining('failed'));
spy.mockRestore();

Mock timers:

import { vi, beforeEach, afterEach } from 'vitest';

beforeEach(() => { vi.useFakeTimers(); });
afterEach(() => { vi.useRealTimers(); });

it('debounces input', async () => {
  const callback = vi.fn();
  const debounced = debounce(callback, 300);

  debounced('hello');
  expect(callback).not.toHaveBeenCalled();

  vi.advanceTimersByTime(300);
  expect(callback).toHaveBeenCalledWith('hello');
});

Mock fetch:

import { vi } from 'vitest';

const mockFetch = vi.fn();
global.fetch = mockFetch;

mockFetch.mockResolvedValueOnce({
  ok: true,
  json: () => Promise.resolve({ id: '1', name: 'Alice' }),
});

Async Testing

it('resolves with user data', async () => {
  const user = await fetchUser('1');
  expect(user).toEqual({ id: '1', name: 'Alice' });
});

it('rejects on not found', async () => {
  await expect(fetchUser('999')).rejects.toThrow('Not found');
});

// Wait for condition
import { waitFor } from '@testing-library/react';
await waitFor(() => {
  expect(screen.getByText('Loaded')).toBeInTheDocument();
});

Testing Hooks

import { renderHook, act } from '@testing-library/react';

it('increments counter', () => {
  const { result } = renderHook(() => useCounter(0));
  expect(result.current.count).toBe(0);

  act(() => result.current.increment());
  expect(result.current.count).toBe(1);
});

Testing with TanStack Query

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

function createWrapper() {
  const queryClient = new QueryClient({
    defaultOptions: { queries: { retry: false } },
  });
  return ({ children }: { children: React.ReactNode }) => (
    <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  );
}

it('displays user data', async () => {
  mockFetch.mockResolvedValueOnce({ ok: true, json: () => ({ name: 'Alice' }) });
  render(<UserProfile userId="1" />, { wrapper: createWrapper() });
  expect(await screen.findByText('Alice')).toBeInTheDocument();
});

Snapshot Testing

it('matches snapshot', () => {
  const { container } = render(<Button variant="primary">Click</Button>);
  expect(container.firstChild).toMatchSnapshot();
});

// Inline snapshot (preferred — visible in test file)
it('serializes config', () => {
  expect(getConfig()).toMatchInlineSnapshot(`
    {
      "debug": false,
      "port": 3000,
    }
  `);
});

Coverage

vitest run --coverage           # run with coverage
vitest --coverage --watch       # watch mode with coverage

Test Pyramid Strategy

         /  E2E  \          Playwright — critical user flows (5-10 tests)
        /----------\
       / Component  \       Vitest + Testing Library — UI behavior (many)
      /--------------\
     /    Unit Tests   \    Vitest — pure functions, utils, hooks (most)
    /--------------------\

Rules:

  • Unit tests: pure functions, utilities, hooks, reducers. Fast, isolated.
  • Component tests: render + interact + assert. Mock external deps, not internal state.
  • E2E tests: critical user paths only (auth flow, checkout, data CRUD). Slow, flaky risk.
  • Test behavior, not implementation. Query by role/text, not class/id.
  • One assertion cluster per test. Multiple related expects OK, but one logical check.
  • Co-locate tests: foo.ts → foo.test.ts in same directory.

CLI Commands

vitest                   # watch mode (default)
vitest run               # single run (CI)
vitest run src/utils     # run tests in specific directory
vitest -t "calculates"   # run tests matching name
vitest --reporter=verbose # detailed output
vitest bench             # run benchmarks

Performance Tips

  • Vitest runs tests in worker threads — parallel by default
  • Use --pool=threads (default) for speed, --pool=forks for isolation
  • --sequence.concurrent to run tests within a file concurrently
  • vi.mock is hoisted automatically — no need for manual hoisting
  • Avoid beforeAll database seeding — use per-test factories instead

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.