# vitest-unit-testing

vitest-unit-testing is a Claude Code rule for writing Vitest unit tests. It guides module mocking and React component test structure.

- rule · `type=rule` · Claude Code, Codex, Cursor, GitHub Copilot, Windsurf (Devin Desktop), Cline · ★ 267 repo stars
- Author: przeprogramowani
- License: AGPL-3.0 · policy=link-only · description only; the file stays with the author
- Source: https://github.com/przeprogramowani/ai-rules-builder/tree/HEAD/.cursor/rules/vitest-unit-testing.mdc
- Updated: 2026-02-20
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/rules/vitest-unit-testing

## What it does

This instruction file defines how Claude Code drafts Vitest suites. It enforces the Arrange-Act-Assert layout alongside descriptive `describe` blocks. For test doubles, it guides the agent toward `vi.spyOn()` when tracking execution and `vi.fn()` or `vi.stubGlobal()` for direct mocks. It also recommends verifying type signatures via `expectTypeOf()`.

Component tests require `environment: 'jsdom'` in `vitest.config.ts` paired with testing library tools. Because Vitest runs `vi.mock()` factories before evaluating imports, placing mock setups at the top level is mandatory.

## Install

### Cursor (native platform)

**Manually: put the files in place**
- File: `.cursor/rules/vitest-unit-testing.mdc` — Frontmatter: description, globs, alwaysApply

### Claude Code

**Manually: put the files in place**
- File: `.claude/rules/vitest-unit-testing.md` — The paths frontmatter field limits the files: move globs from the source into paths

### Codex

**AGENTS.md**
- Codex reads AGENTS.md: paste the rule text there

### GitHub Copilot

**Manually: put the files in place**
- File: `.github/instructions/vitest-unit-testing.instructions.md` — The applyTo field sets which files it applies to: move globs from the source into applyTo

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/rules/vitest-unit-testing.md`

### Cline

**Manually: put the files in place**
- File: `.clinerules/vitest-unit-testing.md`

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/przeprogramowani/ai-rules-builder/tree/HEAD/.cursor/rules/vitest-unit-testing.mdc

## When to use it
- configure test doubles with `vi.fn()` and `vi.spyOn()`
- structure test cases using Arrange-Act-Assert
- set up `jsdom` environments for component testing
- assert TypeScript types using `expectTypeOf()`

## Example prompts
- Write Vitest unit tests for this React button component
- Mock the authentication module at the top level using `vi.mock()`
- Add type assertion tests using `expectTypeOf()`

## Similar
- [vitest-unit-testing-cursorrules-prompt-file](https://affpapa.org/en/nevibecode/rules/vitest-unit-testing-cursorrules-prompt-file.md) — rule · ★ 41178 repo stars
- [vitest-testing](https://affpapa.org/en/nevibecode/skills/oakoss-vitest-testing.md) — skill · 226 installs
- [vitest-testing-patterns](https://affpapa.org/en/nevibecode/skills/vitest-testing-patterns.md) — skill · 373 installs
- [unit-test-generator](https://affpapa.org/en/nevibecode/skills/unit-test-generator.md) — skill · 379 installs
- [vitest](https://affpapa.org/en/nevibecode/skills/claude-mpm-team-vitest.md) — skill · 949 installs
- [vitest-skill](https://affpapa.org/en/nevibecode/skills/vitest-skill.md) — skill · 506 installs

From the same set: [przeprogramowani/ai-rules-builder](https://affpapa.org/en/nevibecode/by/przeprogramowani/ai-rules-builder.md) — 24 cards

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
