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
.claude/rules/testing-patterns.mdWhat 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.envafter a test mutates it - Compare coverage numbers with the config thresholds
Example prompts for the agent
- Add a test for
src/llm/config.tsinsrc/llm/__tests__/config.test.ts, withvi.clearAllMocksinbeforeEach - Run just
npx vitest run src/scoring/__tests__/accuracy.test.tsand tell me which assertion fails - Run
npm run test:coverageand check the result against the thresholds invitest.config.ts
How to install testing-patterns
Manually: put the files in place
.claude/rules/testing-patterns.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/testing-patterns.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/testing-patterns.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/testing-patterns.mdManually: put the files in place
.clinerules/testing-patterns.mdStep 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