agentleFS
luigi-dvluigi-dv/react-ts-vite/.claude/skills/testing-vitest/SKILL.md

testing-vitest

Skill1 starsChanged 26 days agoMITView on GitHub

Write and run tests with Vitest 5, Testing Library and jest-dom in this project — page smoke tests, component behaviour tests, hook tests, service mocks and script tests. Use when adding tests, fixing failing tests, raising coverage or deciding what to test.

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

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    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:

    Prefer testing through a component.

    line 65
  2. Very specific:

    Read the assertion and the rendered DOM (screen.debug()).

    line 78
  3. Distinctive:

    Check the accessible name/role you query against the markup.

    line 79

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 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

Testing with Vitest

Commands

npm test                      # watch mode
npm run test:run              # single run (CI)
npm run test:run -- __tests__/pages   # a folder or file
npm run coverage              # v8 coverage → coverage/index.html

Config lives in vite.config.ts (test block): happy-dom environment at http://localhost:3000/, globals on, vitest.setup.ts loads @testing-library/jest-dom/vitest. Tests are discovered in __tests__/** and src/**/*.test.tsx.

Page smoke test (generated for every template)

import { render, screen } from '@testing-library/react'
import { describe, expect, test } from 'vitest'

import Dashboard from '@pages/Dashboard'

describe('<Dashboard />', () => {
  test('renders the page heading', () => {
    render(<Dashboard />)
    expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent('Overview')
  })
})

Behaviour test

import { fireEvent, render, screen } from '@testing-library/react'

test('shows validation errors on empty submit', () => {
  render(<SignInForm onSubmit={vi.fn()} />)
  fireEvent.click(screen.getByRole('button', { name: /sign in/i }))
  expect(screen.getByText('Email address is required.')).toBeInTheDocument()
})

Query priority: getByRole (with name) → getByLabelText → getByText → getByTestId (last resort). Use findBy* for async UI and queryBy* to assert absence.

Mocking at the boundary

vi.mock('@services/customers', () => ({
  listCustomers: vi.fn().mockResolvedValue([{ id: 1, name: 'Ada' }]),
}))

Or stub fetch: vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify(data)))). Reset in afterEach(() => vi.restoreAllMocks()). Fake timers: vi.useFakeTimers() + vi.advanceTimersByTime(ms).

Hooks

Prefer testing through a component. For isolated logic use renderHook from @testing-library/react and act for updates.

Scripts (bin/, scripts/)

Plain ESM tests in __tests__/scripts/*.test.mjs import the libraries directly (import { checkImports } from '../../scripts/lib/imports.mjs'). Use fs.mkdtempSync for file-system scenarios and never touch the repository itself.

What to test (and not)

  • ✅ Rendering of every page, validation and error states, conditional UI, hooks' observable behaviour, services' request shaping and error mapping, architecture/catalogue integrity.
  • ❌ Tailwind class names, third-party internals (Headless UI focus trapping), snapshots of large trees, implementation details (state variable names).

Failing test triage

  1. Read the assertion and the rendered DOM (screen.debug()).
  2. Check the accessible name/role you query against the markup.
  3. Async? await screen.findBy… or await waitFor(...).
  4. Environment? URL is http://localhost:3000/; window.matchMedia exists in happy-dom but returns matches: false.

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.