Agent skill
add-test
Generate tests for an existing source file using Bun test runner
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/add-test-corbanb-x4-mono
SKILL.md
Add Test Skill
Generate a test file for an existing source file in x4-mono using Bun's built-in test runner.
Arguments
The user provides a source file path. If no path given, ask which file to generate tests for.
Test Patterns by Source Location
| Source | Test Location | Pattern |
|---|---|---|
apps/api/src/routers/*.ts |
apps/api/src/__tests__/*.test.ts |
tRPC caller |
apps/api/src/middleware/*.ts |
apps/api/src/__tests__/*.test.ts |
app.request() |
apps/api/src/lib/*.ts |
apps/api/src/__tests__/*.test.ts |
Direct function calls |
packages/shared/utils/*.ts |
Co-located *.test.ts |
Direct function calls |
packages/shared/types/*.ts |
Co-located *.test.ts |
Zod .safeParse() |
apps/web/src/components/*.tsx |
Co-located *.test.tsx |
@testing-library/react |
tRPC Router Test Template
import { describe, test, expect } from 'bun:test';
import { createCallerFactory } from '../trpc';
import { appRouter } from '../routers';
import type { Context } from '../trpc';
import { createMockDb, createTestUser, TEST_USER_ID } from './helpers';
function createTestContext(overrides: Partial<Context> = {}): Context {
return {
db: createMockDb(),
user: null,
req: new Request('http://localhost:3002'),
...overrides,
};
}
const createCaller = createCallerFactory(appRouter);
describe('routerName', () => {
describe('list', () => {
test('returns items', async () => {
const caller = createCaller(createTestContext());
const result = await caller.routerName.list({ limit: 20, offset: 0 });
expect(result.items).toBeArray();
});
});
describe('create', () => {
test('throws UNAUTHORIZED for unauthenticated user', async () => {
const caller = createCaller(createTestContext({ user: null }));
await expect(caller.routerName.create({ name: 'test' })).rejects.toMatchObject({
code: 'UNAUTHORIZED',
});
});
});
});
What to Test per Procedure/Function
For tRPC routers:
- Happy path with valid input
- Invalid input →
BAD_REQUEST - Unauthenticated →
UNAUTHORIZED(protected procedures) - Wrong owner →
FORBIDDEN - Missing resource →
NOT_FOUND - Edge cases (empty results, pagination boundaries)
For Zod schemas:
- Valid input with all required fields
- Valid input with optional fields omitted
- Each required field missing
- Invalid types for each field
- Boundary values (min/max length, min/max number)
- Default values applied correctly
For utility functions:
- Normal inputs → expected outputs
- Edge cases (empty strings, zero, null)
- Error conditions
Mock Patterns
Database Mock
Use createMockDb() from apps/api/src/__tests__/helpers.ts.
Module Mock
import { mock } from 'bun:test';
mock.module('@x4/database', () => ({ db: createMockDb() }));
CRITICAL: bun mock.module runs ALL test files in the same process. First call wins. Extract shared mocks into a single helper file.
Environment Variables
env.ts validates eagerly at import time — set env vars BEFORE importing:
process.env.DATABASE_URL ??= 'postgres://test:test@localhost/test';
Conventions
- Always import from
bun:test(never jest) - Use
describeblocks grouped by function/procedure name - Use descriptive test names: "returns X when Y", "throws Z for invalid input"
- Use
toMatchObjectfor partial error matching - Test tRPC error codes:
NOT_FOUND,UNAUTHORIZED,FORBIDDEN,BAD_REQUEST
Workflow
- Read the source file to understand exports and logic
- Determine the test pattern based on file location
- Check for existing test helpers in
apps/api/src/__tests__/helpers.ts - Generate the test file at the correct location
- Run
bun test <test-file-path>to verify tests pass
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?