test-gen
Use when unit or integration tests need to be written for a specific file or module.
Test Generator
Auto-generate tests for specified files or modules.
Arguments
/test-gen <file>— generate tests for specific file/test-gen <directory>— tests for all files in directory/test-gen --integration— focus on integration tests/test-gen --coverage— run with coverage report after
Workflow
Step 1: Understand the Target
- Read the target file(s)
- Identify: exports, functions, classes, API endpoints, React components
- Map dependencies and side effects
Step 2: Discover Testing Patterns
- Find test framework (Jest, Vitest, Pytest) from configs
- Find test file location convention from existing tests
- Read 1-2 neighboring test files to match: import style, describe/it structure, mock patterns, assertion style
Step 3: Plan Test Cases
Functions: happy path, edge cases (empty/null/zero/negative), error cases, boundary values
Components: renders, props, interactions, conditional rendering, states
Endpoints: success (200), validation (400), not found (404), auth (401/403)
Classes: constructor, methods, state transitions, errors
Step 4: Generate
- Create test file at correct path (matching convention)
- Write imports matching project style
- Generate grouped test cases with appropriate mocks
- One assertion per test where practical
Step 5: Verify
npx jest <test-file> --no-coverage
Fix failing tests (max 3 iterations) — fix the test, not the source.
Step 6: Report
✅ Generated: src/utils/__tests__/helper.test.ts
- 12 test cases, all passing ✓
Rules
- ALWAYS match existing test patterns in the project
- NEVER modify source code — only test files
- Mock external dependencies, not internal modules
- Meaningful test descriptions
- If no test framework detected, suggest installing one