vitest-unit-testing — Claude Code rule: what it does, how to install
vitest-unit-testing is a Claude Code rule for writing Vitest unit tests. It guides module mocking and React component test structure.
AGPL-3.0 ★ 267 updated 2026-02-20
.cursor/rules/vitest-unit-testing.mdcWhat vitest-unit-testing 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.
What it helps with
- configure test doubles with
vi.fn()andvi.spyOn() - structure test cases using Arrange-Act-Assert
- set up
jsdomenvironments for component testing - assert TypeScript types using
expectTypeOf()
Example prompts for the agent
- 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()
How to install vitest-unit-testing
Manually: put the files in place
.cursor/rules/vitest-unit-testing.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/vitest-unit-testing.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/vitest-unit-testing.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/vitest-unit-testing.mdManually: put the files in place
.clinerules/vitest-unit-testing.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about vitest-unit-testing
How to install vitest-unit-testing in Claude Code?
Add the rule file to your repository at .claude/rules/vitest-unit-testing.md.
What does vitest-unit-testing do?
It instructs Claude Code on formatting Vitest test files, configuring mocks with vi, and arranging test blocks.
Why must vi.mock() be defined at the top level?
Vitest hoists mock factories before evaluating import statements. Defining mocks lower down can cause tests to run against unmocked dependencies.