Skip to main content
rule CX Codex

testing-patterns — Claude Code rule for Vitest tests and setup

testing-patterns is a Claude Code rule for Vitest conventions: test locations, LLM mocks, and running a single spec.

MIT ★ 1,319 updated 2026-09-24

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/testing-patterns.md
Open on GitHub

What testing-patterns does

This rule sets how the agent writes and runs Vitest tests in the repo. Settings come from vitest.config.ts, while src/test/setup.ts loads automatically before a run. The full suite goes through npm run test; a single file runs as npx vitest run src/scoring/__tests__/accuracy.test.ts.

The tricky part is LLM mocking. @anthropic-ai/sdk, openai and @anthropic-ai/vertex-sdk are already stubbed for the whole suite in the setup file, so a test that mocks them again breaks that arrangement. Node built-ins such as fs and child_process get vi.mock(), and vi.mocked() keeps assertions typed. Save process.env in beforeEach, restore it in afterEach.

Scope is set by the frontmatter: only files under src/**/__tests__/** pick the rule up. A spec stored elsewhere never sees it, so colocation is a requirement rather than a preference. Coverage runs with npm run test:coverage on the v8 provider, thresholds sit in vitest.config.ts.

What it helps with

  • Keep new tests beside the source files they cover
  • Stop per-test mocks of the LLM SDKs from drifting
  • Run a single failing spec instead of the whole suite
  • Restore process.env after a test mutates it
  • Compare coverage numbers with the config thresholds

Example prompts for the agent

  • Add a test for src/llm/config.ts in src/llm/__tests__/config.test.ts, with vi.clearAllMocks in beforeEach
  • Run just npx vitest run src/scoring/__tests__/accuracy.test.ts and tell me which assertion fails
  • Run npm run test:coverage and check the result against the thresholds in vitest.config.ts

How to install testing-patterns

Manually: put the files in place

.claude/rules/testing-patterns.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about testing-patterns

How do I install testing-patterns in Claude Code?

Copy the rule file to .claude/rules/testing-patterns.md in your project.

What does testing-patterns do?

It gives the agent the repo's Vitest conventions: file layout, dependency mocks, single-file runs and coverage.

Which files does the rule apply to?

Only paths matching src/**/__tests__/**, as declared in the rule frontmatter.

Source text

testing-patterns.md · MIT
---
paths:
  - src/**/__tests__/**
---

# Testing Patterns

- Config: `vitest.config.ts` · Setup: `src/test/setup.ts` (auto-loaded)
- Run all: `npm run test` · Single: `npx vitest run src/scoring/__tests__/accuracy.test.ts`
- LLM calls globally mocked in `src/test/setup.ts` — never mock `@anthropic-ai/sdk`, `openai`, `@anthropic-ai/vertex-sdk` in individual tests
- Use `vi.mock()` for `fs`, `child_process` · `vi.mocked()` for type-safe assertions
- Colocated: `src/llm/__tests__/config.test.ts` tests `src/llm/config.ts`
- Pattern: `describe` → `beforeEach(vi.clearAllMocks)` → `it` with `expect`
- Env vars: save `process.env` in `beforeEach`, restore in `afterEach`
- Coverage: `npm run test:coverage` (v8 provider, thresholds in `vitest.config.ts`)

© caliber-ai-org · MIT · caliber-ai-org/ai-setup

Similar IDE rules

All IDE rules for Testing and QA →

From the same set17

The whole set →
caliber-onboarding caliber-onboarding — a rule for Claude Code and Codex that checks whether Caliber configs are synced before the agent responds to your first message. If sync is not set up, it offers to run /setup-caliber in about 30 seconds. CX Codex +1 ★ 1,319repo stars llm-layer llm-layer pins the conventions for model-provider code: the LLMProvider interface, config lookup, seat-based handling and usage tracking. Scoped to src/llm/**. CX Codex +1 ★ 1,319repo stars testing Defines Vitest testing patterns for Caliber, enforcing colocated test suites, environment isolation, and global LLM mocks. CX Codex +1 ★ 1,319repo stars caliber-conventions The caliber-conventions rule sets conventions for Caliber TypeScript CLI development. Use it to guide an agent working on project code, tests, and commits. CX Codex +1 ★ 1,319repo stars scoring-patterns scoring-patterns defines coding rules for the src/scoring/** directory. It keeps scoring checks deterministic without external network calls or random values. CX Codex +1 ★ 1,319repo stars llm-patterns The llm-patterns rule sets patterns for LLM providers in a project. Use it when provider interfaces, model settings, CLI errors, or streamed responses need consistent handling. CX Codex +1 ★ 1,319repo stars caliber-pre-commit caliber-pre-commit — a commit-time rule: it checks for the Caliber git hook and, when the hook is absent, syncs and stages your agent config files. CX Codex +1 ★ 1,319repo stars find-skills find-skills discovers community skills from the Caliber registry and installs them directly into your coding session. CX Codex +1 ★ 1,319repo stars

Project sponsors

Companies that keep this analytics open