caliber-testing
Skill1.3k starsChanged 7 months agoMITView on GitHub
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.
- Reads credentials
Trending
Less talk lately.about the whole repository
Who's talking about caliber-ai-org/ai-setup
- sudosubin/agents.nix“agent-skills.github.caliber-ai-org.ai-setup: 1.55.1 → 1.55.2”GitHub · Oct 3, 2026
- FlorianBruniaux/claude-code-ultimate-guide“Broken links detected in guide markdown”GitHub · Oct 1, 2026
- sudosubin/agents.nix“agent-skills.github.caliber-ai-org.ai-setup: init at 1.55.1”GitHub · Sep 26, 2026
- FlorianBruniaux/claude-code-ultimate-guide“Broken links detected in guide markdown”GitHub · Sep 2, 2026
- FlorianBruniaux/claude-code-ultimate-guide“Broken links detected in guide markdown”GitHub · Sep 1, 2026
The biggest 5 of 22 mentions linking to the repository that our crawl found. Each line is their own headline.
See all 22
Talked about on GitHub (22 times).
On GitHub · 22
- Oct 3, 2026agent-skills.github.caliber-ai-org.ai-setup: 1.55.1 → 1.55.2 — sudosubin/agents.nix0 reactions
- Oct 1, 2026Broken links detected in guide markdown — FlorianBruniaux/claude-code-ultimate-guide0 reactions
- Sep 26, 2026agent-skills.github.caliber-ai-org.ai-setup: init at 1.55.1 — sudosubin/agents.nix0 reactions
- Sep 2, 2026Broken links detected in guide markdown — FlorianBruniaux/claude-code-ultimate-guide0 reactions · 1 comment
- Sep 1, 2026Broken links detected in guide markdown — FlorianBruniaux/claude-code-ultimate-guide0 reactions · 1 comment
17 more on GitHub
- Sep 1, 2026Broken links detected in guide markdown — AMusso19/CUSTOM-claude-code-ultimate-guide-Florian0 reactions
- Sep 1, 2026Broken links detected in guide markdown — alexica00/claude-code-ultimate-guide0 reactions
- Aug 1, 2026Broken links detected in guide markdown — AMusso19/CUSTOM-claude-code-ultimate-guide-Florian0 reactions
- Aug 1, 2026Broken links detected in guide markdown — alexica00/claude-code-ultimate-guide0 reactions
- Aug 1, 2026Broken links detected in guide markdown — FlorianBruniaux/claude-code-ultimate-guide0 reactions · 1 comment
- Jul 1, 2026Broken links detected in guide markdown — AMusso19/CUSTOM-claude-code-ultimate-guide-Florian0 reactions
- Jul 1, 2026Broken links detected in guide markdown — alexica00/claude-code-ultimate-guide0 reactions
- Jul 1, 2026Broken links detected in guide markdown — FlorianBruniaux/claude-code-ultimate-guide0 reactions · 1 comment
- Jun 1, 2026Broken links detected in guide markdown — alexica00/claude-code-ultimate-guide0 reactions
- Jun 1, 2026Broken links detected in guide markdown — FlorianBruniaux/claude-code-ultimate-guide0 reactions · 1 comment
- May 18, 2026Broken links detected in guide markdown — FlorianBruniaux/claude-code-ultimate-guide0 reactions · 1 comment
- May 18, 2026Broken links detected in guide markdown — FlorianBruniaux/claude-code-ultimate-guide0 reactions · 1 comment
- May 18, 2026Broken links detected in guide markdown — FlorianBruniaux/claude-code-ultimate-guide0 reactions · 1 comment
- May 18, 2026Broken links detected in guide markdown — FlorianBruniaux/claude-code-ultimate-guide0 reactions · 1 comment
- Apr 18, 2026Audit candidate: caliber-ai-org/ai-setup — xiaolai/nlpm0 reactions · 4 comments
- Mar 28, 2026Add Caliber — AI config generation & sync CLI — roboco-io/awesome-vibecoding0 reactions · 1 comment
- Mar 26, 2026feat: charter score — zero-install AI-readiness audit for any repo — Stackbilt-dev/charter0 reactions · 1 comment
In this repository38 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .cursor/rules/caliber-conventions.mdcCursor rule
- .cursor/rules/caliber-learnings.mdcCursor rule
- .cursor/rules/caliber-pre-commit.mdcCursor rule
- .cursor/rules/llm-layer.mdcCursor rule
- .cursor/rules/testing.mdcCursor rule
- adding-a-commandSkill
- caliber-testingSkill
- find-skillsSkill
- llm-providerSkill
- save-learningSkill
- scoring-checksSkill
- setup-caliberSkill
- writers-patternSkill
- adding-a-commandSkill
- caliber-testingSkill
- find-skillsSkill
- llm-providerSkill
- save-learningSkill
- scoring-checksSkill
- setup-caliberSkill
- writers-patternSkill
- adding-a-commandSkill
- caliber-testingSkill
- find-skillsSkill
- llm-providerSkill
- save-learningSkill
- scoring-checksSkill
- setup-caliberSkill
- writers-patternSkill
- adding-a-commandSkill
- find-skillsSkill
- llm-providerSkill
- save-learningSkill
- scoring-checksSkill
- setup-caliberSkill
- writers-patternSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
31 rules. About 0 are common, 12 are distinctive, and 19 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈12
Distinctive · estimate
This author's own choices. Worth a look.
19
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- could be automatic: a setting could enforce it
- Very specific: line 11
All test files MUST be placed in __tests__/ directories parallel to source files: src/[module]/__tests__/[module].test.ts
- Very specific: line 13
NEVER mock src/test/setup.ts — it is the global LLM provider mock already applied to all tests
- Very specific: line 16
When a test requires unmocking modules mocked in global setup, call vi.unmock('../module.js') BEFORE the import statement
- Very specific: line 17
Run pnpm test locally and pnpm test:coverage before committing to verify coverage thresholds (lines: 50, functions: 50, branches: 50, …
- Very specific: line 22
Create src/[module]/__tests__/[module].test.ts.
- Distinctive: line 32
Add additional imports based on what you're testing:
- Distinctive: line 37
Verify: All required test utilities are imported before test definitions.
- Distinctive: line 40
If testing a module that imports other modules you want to mock:
- Very specific: line 65
Place all vi.mock() and vi.unmock() calls BEFORE importing the module under test.
- Very specific: line 131
Verify: All temporary directories are pushed to a cleanup array and removed in afterEach.
- Distinctive: line 134
Before each test, clear mock call history to avoid pollution between tests:
- Very specific: line 150
Write assertions using expect().
- Distinctive: line 172
Run tests locally before committing:
- Distinctive: line 179
Verify: All tests pass and coverage thresholds are met (lines: 50%, functions: 50%, branches: 50%).
- Distinctive: line 189
Import test framework and fs module
- Distinctive: line 192
Delete specific env vars to test absence
- Distinctive: line 193
Test each branch: env vars set, file exists, both, neither
- Distinctive: line 238
Create temp dirs in each test and push to cleanup array
- Distinctive: line 240
Test with various file mtimes and directory structures
- Very specific: line 277
Use vi.unmock('../index.js') to test the real module
- Very specific: line 278
Create mock classes in vi.hoisted()
- Distinctive: line 280
Test provider selection logic
- Very specific: line 281
Use resetProvider() between tests to clear cached instances
- Very specific: line 337
Fix: Create a copy in beforeEach: process.env = { ...originalEnv }.
- Very specific: line 341
Fix: Use centralized cleanup array: dirs: string[] = [] pushed to in tests, cleaned in afterEach with fs.rmSync(..., { recursive: true, …
- Very specific: line 345
Fix: Add vi.clearAllMocks() as first statement in beforeEach
- Very specific: line 349
Fix: Ensure the module is mocked with vi.mock() before importing, then cast: const mockFn = vi.mocked(importedFn)
- Very specific: line 353
Fix: Check if temp files are being created in real directories instead of mocked ones.
- Very specific: line 357
Fix: Run pnpm test:coverage locally to see untested lines.
- Very specific: line 358
Example: If if (x) return 'a'; else return 'b'.
- Very specific: line 366
Fix: Ensure vi.hoisted() block returns all factories, and vi.mock() blocks use them after the hoisted definition
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
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:
import { describe, it, expect, vi, beforeEach, afterEach } 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:
vi.mock('../config.js', () => ({
loadConfig: vi.fn(),
writeConfigFile: vi.fn(),
}));For complex mocks with test-time factory functions (hoisted):
const { mockLoadConfig } = vi.hoisted(() => ({
mockLoadConfig: vi.fn(),
}));
vi.mock('../config.js', () => ({
loadConfig: () => mockLoadConfig(),
}));For unmocking global setup mocks (e.g., to test llm/index.js itself):
vi.unmock('../index.js');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():
describe('functionName', () => {
it('returns X when Y', () => {
// test body
});
});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:
describe('config tests', () => {
const originalEnv = process.env;
const originalArgv = process.argv;
beforeEach(() => {
process.env = { ...originalEnv }; // Copy, not reference
process.argv = [...originalArgv];
delete process.env.SPECIFIC_VAR; // Explicitly remove vars to test absence
});
afterEach(() => {
process.env = originalEnv;
process.argv = originalArgv;
});
it('tests env var behavior', () => {
process.env.MY_VAR = 'test';
// test code
});
});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:
describe('file tree', () => {
const dirs: string[] = [];
afterEach(() => {
for (const d of dirs) {
try { fs.rmSync(d, { recursive: true, force: true }); } catch {}
}
dirs.length = 0;
});
it('processes files', () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'caliber-test-'));
dirs.push(tmp);
// use tmp directory
});
});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:
beforeEach(() => {
vi.clearAllMocks(); // Reset all mock call counts and return values
});
afterEach(() => {
vi.restoreAllMocks(); // Restore original implementations
vi.resetModules(); // Reset cached module imports (if you reload modules)
});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:
// Simple checks
expect(value).toBe(expected);
expect(array).toContain(item);
expect(fn).toThrow('error message');
// Instance checks
expect(obj).toBeInstanceOf(ClassName);
// Mock checks
expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith(arg);
// File system checks
expect(fs.existsSync(path)).toBe(true);Verify: Each test has at least one assertion and uses appropriate expect() matchers.
Step 9: Run tests
Run tests locally before committing:
pnpm test # Run all tests in watch mode
pnpm test:coverage # Check coverage thresholds
pnpm test -- src/my/path/__tests__/my.test.ts # Run single test fileVerify: 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:
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
vi.mock('fs');
vi.mock('os', () => ({ default: { homedir: () => '/home/user' } }));
import { loadConfig } from '../config.js';
describe('config', () => {
const originalEnv = process.env;
beforeEach(() => {
vi.clearAllMocks();
process.env = { ...originalEnv };
delete process.env.ANTHROPIC_API_KEY;
delete process.env.OPENAI_API_KEY;
});
afterEach(() => {
process.env = originalEnv;
});
it('returns env config when ANTHROPIC_API_KEY is set', () => {
process.env.ANTHROPIC_API_KEY = 'sk-ant-test';
const config = loadConfig();
expect(config?.provider).toBe('anthropic');
});
it('returns null when no env vars set', () => {
expect(loadConfig()).toBeNull();
});
});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:
import { describe, it, expect, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { getFileTree } from '../file-tree.js';
const dirs: string[] = [];
afterEach(() => {
for (const d of dirs) {
try { fs.rmSync(d, { recursive: true, force: true }); } catch {}
}
dirs.length = 0;
});
describe('getFileTree', () => {
it('returns files sorted by mtime descending', () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'caliber-ft-'));
dirs.push(tmp);
fs.writeFileSync(path.join(tmp, 'a.ts'), 'a');
fs.writeFileSync(path.join(tmp, 'b.ts'), 'b');
const tree = getFileTree(tmp);
expect(tree).toHaveLength(2);
});
});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:
import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.unmock('../index.js');
const { mockLoadConfig, MockAnthropicProvider } = vi.hoisted(() => {
class MockAnthropicProvider {
config: unknown;
call = vi.fn();
constructor(c: unknown) { this.config = c; }
}
return {
mockLoadConfig: vi.fn(),
MockAnthropicProvider,
};
});
vi.mock('../config.js', () => ({
loadConfig: () => mockLoadConfig(),
}));
vi.mock('../anthropic.js', () => ({
AnthropicProvider: MockAnthropicProvider,
}));
import { getProvider, resetProvider } from '../index.js';
describe('getProvider', () => {
beforeEach(() => {
vi.clearAllMocks();
resetProvider();
});
it('creates AnthropicProvider for anthropic config', () => {
mockLoadConfig.mockReturnValue({
provider: 'anthropic',
model: 'claude-sonnet-4-6',
apiKey: 'sk-test',
});
const provider = getProvider();
expect(provider).toBeInstanceOf(MockAnthropicProvider);
});
});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()
Mock factory returns undefined
- Cause:
vi.hoisted()variables used before definition in the same block - Fix: Ensure
vi.hoisted()block returns all factories, andvi.mock()blocks use them after the hoisted definition
Test flakes (sometimes passes, sometimes fails)
- Cause: Timing issues or file system race conditions in cleanup
- Fix: For file cleanup, use
{ force: true }inrmSync. For timing, avoidsetTimeout; usevi.useFakeTimers()andvi.runAllTimers()if needed. Check for leftover files from previous test runs inbeforeEach
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

