caliber-ai-org/ai-setup/caliber-testing
Claude Code and agent skill by caliber-ai-org/ai-setup
- community
Checking that the SKILL.md file is still in the repository.
Writes Vitest tests following project patterns: __tests__/ directories, vi.mock() for module mocking with vi.hoisted() for test-time factories, global LLM mock from src/test/setup.ts, environment variable save/restore in beforeEach/afterEach, vi.clearAllMocks() lifecycle, and test file organization. Use when user says 'write tests', 'add test coverage', 'test this', creates *.test.ts files, or when test failures appear in CI. Do NOT use for non-test code or for debugging without writing tests.
Install
npx vskill@latest install caliber-ai-org/ai-setup/caliber-testingSKILL.md
The same SKILL.md was published earlier as caliber-ai-org/ai-setup/caliber-testing.
Caliber Testing
Critical
- All test files MUST be placed in
__tests__/directories parallel to source files:src/[module]/__tests__/[module].test.ts - Register any new
__tests__/directories invitest.config.ts'sincludeglob (already configured:src/**/*.test.ts) - NEVER mock
src/test/setup.ts— it is the global LLM provider mock already applied to all tests - Environment variable tests MUST save
process.envinbeforeEach, restore inafterEach, and explicitly delete env vars to test absence - Temporary file/directory cleanup MUST happen in
afterEach, not in individual test cleanup. Usefs.rmSync(dir, { recursive: true, force: true }) - When a test requires unmocking modules mocked in global setup, call
vi.unmock('../module.js')BEFORE the import statement - Run
pnpm testlocally andpnpm test:coveragebefore committing to verify coverage thresholds (lines: 50, functions: 50, branches: 50, statements: 50)
Instructions
Step 1: Create the test file in the correct directory
Create src/[module]/__tests__/[module].test.ts. The parent source file is src/[module]/[module].ts.
Verify: The __tests__ directory exists at the same level as the source file being tested.
Step 2: Import test framework
At the top of every test file, import from vitest:
Add additional imports based on what you're testing:
- File system:
import fs from 'fs'; import path from 'path'; import os from 'os'; - Temporary files: Use
fs.mkdtempSync(path.join(os.tmpdir(), 'caliber-prefix-')) - Exec:
import { execSync } from 'child_process';
Verify: All required test utilities are imported before test definitions.
Step 3: Set up module mocking (if needed)
If testing a module that imports other modules you want to mock:
For complex mocks with test-time factory functions (hoisted):
For unmocking global setup mocks (e.g., to test llm/index.js itself):
Place all vi.mock() and vi.unmock() calls BEFORE importing the module under test.
Verify: Mock declarations appear before the import of the module being tested.
Step 4: Organize tests in describe blocks
Group related tests with describe():
Verify: Each it() test has a clear, complete assertion.
Step 5: Manage environment and process state
For tests that modify process.env or process.argv:
Verify: beforeEach creates a copy of env/argv; afterEach restores originals; unused env vars are explicitly deleted with delete process.env.VAR.
Step 6: Manage temporary files and directories
For file system tests, create temporary directories and clean them up:
Verify: All temporary directories are pushed to a cleanup array and removed in afterEach.
Step 7: Handle mock state cleanup
Before each test, clear mock call history to avoid pollution between tests:
Verify: beforeEach calls vi.clearAllMocks() for providers and afterEach calls vi.restoreAllMocks().
Step 8: Test assertions
Write assertions using expect(). Match the patterns from existing tests:
Verify: Each test has at least one assertion and uses appropriate expect() matchers.
Step 9: Run tests
Run tests locally before committing:
Verify: All tests pass and coverage thresholds are met (lines: 50%, functions: 50%, branches: 50%).
Examples
Example 1: Testing a module with environment variables (config.test.ts pattern)
User asks: "Write tests for my config loader that reads from env vars and a config file."
Actions taken:
- Create
src/lib/__tests__/config.test.ts - Import test framework and fs module
- Mock
fsandos - Save/restore
process.envin beforeEach/afterEach - Delete specific env vars to test absence
- Test each branch: env vars set, file exists, both, neither
Result:
Example 2: Testing file system operations with temp cleanup (file-tree.test.ts pattern)
User asks: "Write tests for file tree analysis."
Actions taken:
- Create
src/fingerprint/__tests__/file-tree.test.ts - Set up a cleanup array for temp directories
- Create temp dirs in each test and push to cleanup array
- Clean up all temp dirs in afterEach
- Test with various file mtimes and directory structures
Result:
Example 3: Testing with module mocking and factory functions (index.test.ts pattern)
User asks: "Write tests for the provider factory that instantiates different providers based on config."
Actions taken:
- Create
src/llm/__tests__/index.test.ts - Use
vi.unmock('../index.js')to test the real module - Create mock classes in
vi.hoisted() - Mock dependencies (config, provider classes)
- Test provider selection logic
- Use
resetProvider()between tests to clear cached instances
Result:
Common Issues
"Cannot find module" when running tests
- Cause:
vi.mock()called after the import statement - Fix: Move all
vi.mock()andvi.unmock()calls to the TOP of the file, before anyimportstatements
Environment variable persists across tests
- Cause:
beforeEachassigns by reference instead of copying:process.env = originalEnv - Fix: Create a copy in
beforeEach:process.env = { ...originalEnv }; delete process.env.VAR
Temporary files not cleaned up, filling disk
- Cause:
afterEachnot called or temp paths not tracked - Fix: Use centralized cleanup array:
dirs: string[] = []pushed to in tests, cleaned inafterEachwithfs.rmSync(..., { recursive: true, force: true })
Mock return value from previous test bleeds into next test
- Cause:
vi.clearAllMocks()not called inbeforeEach - Fix: Add
vi.clearAllMocks()as first statement inbeforeEach
"vi.mocked() is not a function" when accessing mock calls
- Cause: Trying to use
vi.mocked()on a non-mocked module - Fix: Ensure the module is mocked with
vi.mock()before importing, then cast:const mockFn = vi.mocked(importedFn)
Test passes locally but fails in CI
- Cause: Tests rely on real file system or network (not mocked)
- Fix: Check if temp files are being created in real directories instead of mocked ones. Verify all external calls use
vi.mock()for fs/http/exec
Coverage threshold failures: "lines not covered", "statements not covered"
- Cause: Branches or error paths not tested
- Fix: Run
pnpm test:coveragelocally to see untested lines. Addit()tests for error cases, edge conditions, and branches that return different values - Example: If
if (x) return 'a'; else return 'b';has 0% branch coverage, add tests: one with x=true, one with x=false
"expected 1 error but got 0" when testing error throws
- Cause: Error not actually thrown, or thrown asynchronously
- Fix: For sync functions:
expect(() => fn()).toThrow('message'). For async:expect(async () => { await fn() }).rejects.toThrow('message')or useawait expect(promise).rejects.toThrow()
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/caliber-testing/SKILL.md
Popularity
- Repository stars
- 1,075
- Repository forks
- 110
- Repository npm downloads, 7 days
- 1,445
- 7-day trend score
- 52
Stars and forks are for the whole GitHub repository, not this skill alone. Downloads are for the repository's npm package. Refreshed 21 May 2026, 6 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/caliber-ai-org/ai-setup/caliber-testing)