Home › Testing › Vitest unit testing AGENTS.md

Vitest unit testing AGENTS.md

AGENTS.md for unit and component tests with Vitest 3 and Testing Library in Vite/TypeScript projects.

AGENTS.mdTesting

How to use

Save this as AGENTS.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

AGENTS.mdDownload  
# AGENTS.md: Testing with Vitest

## Commands
- `npx vitest run` (CI) · `npx vitest <pattern>` (watch) · `npx vitest run --coverage`

## Where and how
- Tests sit next to code: `foo.ts` -> `foo.test.ts`. Component tests use `@testing-library/react` (or `/vue`, `/svelte`) with `@testing-library/user-event`.
- Arrange / act / assert, one behavior per test, and names that read as a sentence: `it('shows an error when the email is invalid')`.
- Query the DOM like a user: `getByRole`, `getByLabelText`. Use `findBy*` for async UI; avoid `container.querySelector`.
- Interact with `const user = userEvent.setup(); await user.click(...)`, not `fireEvent`.
- Mock at the boundary: network with MSW (`msw/node`), modules with `vi.mock` only for things you don't own. Reset with `vi.restoreAllMocks()` in `afterEach` (or `restoreMocks: true` in config).
- Time: `vi.useFakeTimers()` + `vi.setSystemTime()`; restore real timers after.
- Use `test.each` for tables of inputs.
- Snapshot tests only for small, stable output; review snapshot diffs instead of blindly updating with `-u`.

## Rules for agents
- When fixing a bug, write a failing test first, then the fix.
- Never delete, skip (`.skip`), or weaken an assertion to make a test pass. If a test looks wrong, say so.
- Don't test implementation details (internal state, private functions).

## Done
`npx vitest run` passes and new code paths have tests.

Related rules

REST API design AGENTS.md

AGENTS.md for designing consistent HTTP/JSON APIs: resource naming, status codes, errors, pagination, and versioning.

AGENTS.mdAPI design

Bevy game engine AGENTS.md

AGENTS.md for Bevy (0.16+) games in Rust: ECS systems, plugins, required components, and fast dev builds.

AGENTS.mdBevy (Rust)

Git commits & PRs AGENTS.md

AGENTS.md rules for how agents commit: Conventional Commits, atomic changes, safe git commands, and PR descriptions.

AGENTS.mdAgent workflows

CrewAI multi-agent AGENTS.md

AGENTS.md for CrewAI 1.x projects: YAML agents and tasks, crews and flows, tools, and cost-aware runs.

AGENTS.mdLLM apps & agents

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.