Skip to main content
rule CX Codex

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

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/vitest-unit-testing.mdc
Open on GitHub

What 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() and vi.spyOn()
  • structure test cases using Arrange-Act-Assert
  • set up jsdom environments 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.mdc

Frontmatter: description, globs, alwaysApply

Step 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.

Similar IDE rules

All IDE rules for Testing and QA →

From the same set24

The whole set →
shadcn-plan-summary shadcn-plan-summary — a command that captures decisions from UI architecture sessions. Extracts choices about layout, flows, API integration, and state management, then compiles them into a structured report for the next development phase. CX Codex +1 ★ 267repo stars test-plan test-plan is a Claude Code rule that builds a project-specific test plan from the codebase and tech stack. It writes the result in Polish as Markdown. CX Codex +1 ★ 267repo stars supabase-migrations This rule guides Claude Code when creating Supabase SQL migrations, from timestamped filenames to RLS policies. It applies to files under supabase/migrations/. CX Codex +1 ★ 267repo stars generate-types-from-supabase generate-types-from-supabase analyzes Supabase schema and generates TypeScript types for DTOs and Command Models. Cuts down manual type definition work by deriving structures from your database. CX Codex +1 ★ 267repo stars project-prd The project-prd command turns project plans into a structured PRD and saves it as a Markdown file. Use it to draft requirements, user stories, and acceptance criteria. CX Codex +1 ★ 267repo stars 10x-implement 10x-implement — a command to implement approved technical plans step-by-step in Claude Code. Reads the plan, tracks progress, and verifies each phase before moving on. CX Codex +1 ★ 267repo stars shadcn-plan-session shadcn-plan-session runs an interactive UI planning session for an MVP using product specs, tech stack notes, and API endpoints. CX Codex +1 ★ 267repo stars shadcn-view-implement The shadcn-view-implement command guides Claude Code through building a shadcn/ui view from an implementation plan. It pauses after a small batch of work so you can review the result. CX Codex +1 ★ 267repo stars

Project sponsors

Companies that keep this analytics open