Generate Tests — Comprehensive Test Suite Generator
Generate complete test coverage for any file, component, or module. Covers unit tests, integration tests, edge cases, error handling, and mocking — adapted to whatever testing framework the project uses.
Use when: you want tests generated for a file or component, need to improve test coverage, want edge case coverage, need mocks for external dependencies, or want to bootstrap a test suite for untested code.
When This Skill Is Activated
Follow every step below. Do NOT generate tests without first analyzing the project's testing setup and the target code.
Step 1: Detect the Testing Setup
Before writing any tests, discover the project's testing conventions:
Find the test framework — Check package.json (dependencies/scripts), or config files:
jest.config.* / jest key in package.json → Jest
vitest.config.* → Vitest
cypress.config.* → Cypress
playwright.config.* → Playwright
pytest.ini / pyproject.toml [tool.pytest] → pytest
go.mod → Go testing
Cargo.toml → Rust (#[cfg(test)])
Find existing test files — Search for *.test.*, *.spec.*, test_*.py, *_test.go to understand naming conventions and patterns already in use.
Check for test utilities — Look for shared helpers, factories, custom matchers, or mock setups the project already provides (e.g., test/utils.ts, __mocks__/, conftest.py).
Check the test script — Read the test script in package.json (or equivalent) to understand how tests are run, what flags are used, and what coverage tool is configured.
Match the project's existing conventions exactly. File naming, import style, assertion style, describe/it vs test, etc.
Step 2: Analyze the Target Code
Read the file or component the user wants tested. Identify:
- All exported functions, classes, methods, and components
- Input types and return types (or infer them)
- External dependencies (API calls, database, file system, third-party libs)
- Side effects (mutations, network calls, timers, DOM manipulation)
- Error paths (throw statements, catch blocks, error returns)
- Edge cases (null/undefined inputs, empty arrays, boundary values, large inputs)
- Async behavior (promises, callbacks, streams)
Step 3: Plan the Test Suite
Before writing code, outline what you'll test:
Unit Tests
- Every exported function with representative inputs
- Return values for happy path
- Error handling for invalid inputs
- Boundary values (0, -1, empty string, null, max int, etc.)
- Type coercion edge cases if applicable
Integration Tests (if the code interacts with other modules)
- Component interactions and data flow
- API calls with mocked responses (success + failure)
- Service layer with mocked dependencies
- State management side effects
Edge Cases and Error Handling
- Null, undefined, empty inputs
- Malformed data
- Network failures / timeouts
- Concurrent access / race conditions
- Extremely large inputs
Step 4: Write the Tests
Follow these principles:
Structure
describe("[ModuleName]", () => {
describe("[functionName]", () => {
it("should [expected behavior] when [condition]", () => {
// Arrange
// Act
// Assert
});
});
});
- Descriptive test names — Read like documentation: "should return empty array when input is null"
- AAA pattern — Arrange (setup), Act (execute), Assert (verify). Separate each visually.
- One assertion per concept — Multiple
expect() calls are fine if they assert one logical thing
- Group with
describe — One block per function/method/component behavior
- Setup/teardown — Use
beforeEach/afterEach for test isolation. Clean up subscriptions, timers, mocks.
Mocking Strategy
- Mock external dependencies only — Don't mock the code under test
- Use the project's existing mock patterns (e.g.,
jest.mock(), vi.mock(), unittest.mock)
- Create test data factories for complex objects instead of inline literals
- Mock timers (
jest.useFakeTimers() / vi.useFakeTimers()) for time-dependent code
- Mock dates for deterministic snapshots
- Always restore mocks in
afterEach to prevent test pollution
Async Testing
- Always
await async functions or return the promise
- Test both resolve and reject paths
- Use
waitFor / findBy for async UI updates (React Testing Library)
- Test loading, success, and error states
Framework-Specific Guidance
| Framework |
Component Testing |
Key Patterns |
| React |
React Testing Library |
render(), screen.getByRole(), userEvent, waitFor |
| Vue |
Vue Test Utils |
mount(), shallowMount(), wrapper.find(), trigger() |
| Angular |
TestBed |
TestBed.configureTestingModule(), fixture.detectChanges() |
| Node.js |
Supertest |
request(app).get("/api/...").expect(200) |
| Python |
pytest |
@pytest.fixture, monkeypatch, parametrize |
| Go |
testing |
t.Run(), table-driven tests, t.Parallel() |
Step 5: Verify and Improve
After generating the tests:
- Run the tests — Execute the test suite to make sure they pass
- Check coverage — Identify untested lines or branches
- Add missing cases — Fill gaps in coverage, especially error paths
- Review test quality — Tests should fail when the code breaks, not just when the tests break
Coverage Goals
| Priority |
Coverage Target |
| Critical business logic |
90%+ |
| Utility functions |
85%+ |
| UI components |
80%+ |
| Configuration/glue code |
60%+ |
Focus on branch coverage over line coverage — untested else and catch paths are where bugs hide.
Output Checklist
Before finishing, verify:
1---2name: generate-tests3description: "Use when asked to generate tests for a file, module or component, to raise coverage, or to bootstrap a suite for untested code. Detects the project's test framework and conventions first, then writes unit, integration and edge-case tests."4domain: engineering5supports: [claude-code, cursor, codex, generic]6version: 0.1.07metadata:8 source: https://skillmd.com/skills/coco-research/generate-tests9---1011# Generate Tests — Comprehensive Test Suite Generator1213Generate complete test coverage for any file, component, or module. Covers unit tests, integration tests, edge cases, error handling, and mocking — adapted to whatever testing framework the project uses.1415**Use when**: you want tests generated for a file or component, need to improve test coverage, want edge case coverage, need mocks for external dependencies, or want to bootstrap a test suite for untested code.1617---1819## When This Skill Is Activated2021Follow every step below. Do NOT generate tests without first analyzing the project's testing setup and the target code.2223---2425## Step 1: Detect the Testing Setup2627Before writing any tests, discover the project's testing conventions:28291. **Find the test framework** — Check `package.json` (dependencies/scripts), or config files:30 - `jest.config.*` / `jest` key in `package.json` → Jest31 - `vitest.config.*` → Vitest32 - `cypress.config.*` → Cypress33 - `playwright.config.*` → Playwright34 - `pytest.ini` / `pyproject.toml` [tool.pytest] → pytest35 - `go.mod` → Go testing36 - `Cargo.toml` → Rust (#[cfg(test)])37382. **Find existing test files** — Search for `*.test.*`, `*.spec.*`, `test_*.py`, `*_test.go` to understand naming conventions and patterns already in use.39403. **Check for test utilities** — Look for shared helpers, factories, custom matchers, or mock setups the project already provides (e.g., `test/utils.ts`, `__mocks__/`, `conftest.py`).41424. **Check the test script** — Read the `test` script in `package.json` (or equivalent) to understand how tests are run, what flags are used, and what coverage tool is configured.4344**Match the project's existing conventions exactly.** File naming, import style, assertion style, describe/it vs test, etc.4546---4748## Step 2: Analyze the Target Code4950Read the file or component the user wants tested. Identify:5152- All exported functions, classes, methods, and components53- Input types and return types (or infer them)54- External dependencies (API calls, database, file system, third-party libs)55- Side effects (mutations, network calls, timers, DOM manipulation)56- Error paths (throw statements, catch blocks, error returns)57- Edge cases (null/undefined inputs, empty arrays, boundary values, large inputs)58- Async behavior (promises, callbacks, streams)5960---6162## Step 3: Plan the Test Suite6364Before writing code, outline what you'll test:6566### Unit Tests67- Every exported function with representative inputs68- Return values for happy path69- Error handling for invalid inputs70- Boundary values (0, -1, empty string, null, max int, etc.)71- Type coercion edge cases if applicable7273### Integration Tests (if the code interacts with other modules)74- Component interactions and data flow75- API calls with mocked responses (success + failure)76- Service layer with mocked dependencies77- State management side effects7879### Edge Cases and Error Handling80- Null, undefined, empty inputs81- Malformed data82- Network failures / timeouts83- Concurrent access / race conditions84- Extremely large inputs8586---8788## Step 4: Write the Tests8990Follow these principles:9192### Structure9394```95describe("[ModuleName]", () => {96 describe("[functionName]", () => {97 it("should [expected behavior] when [condition]", () => {98 // Arrange99 // Act100 // Assert101 });102 });103});104```105106- **Descriptive test names** — Read like documentation: "should return empty array when input is null"107- **AAA pattern** — Arrange (setup), Act (execute), Assert (verify). Separate each visually.108- **One assertion per concept** — Multiple `expect()` calls are fine if they assert one logical thing109- **Group with `describe`** — One block per function/method/component behavior110- **Setup/teardown** — Use `beforeEach`/`afterEach` for test isolation. Clean up subscriptions, timers, mocks.111112### Mocking Strategy113114- Mock **external dependencies only** — Don't mock the code under test115- Use the project's existing mock patterns (e.g., `jest.mock()`, `vi.mock()`, `unittest.mock`)116- Create **test data factories** for complex objects instead of inline literals117- Mock timers (`jest.useFakeTimers()` / `vi.useFakeTimers()`) for time-dependent code118- Mock dates for deterministic snapshots119- Always **restore mocks** in `afterEach` to prevent test pollution120121### Async Testing122123- Always `await` async functions or return the promise124- Test both resolve and reject paths125- Use `waitFor` / `findBy` for async UI updates (React Testing Library)126- Test loading, success, and error states127128### Framework-Specific Guidance129130| Framework | Component Testing | Key Patterns |131|-----------|------------------|--------------|132| **React** | React Testing Library | `render()`, `screen.getByRole()`, `userEvent`, `waitFor` |133| **Vue** | Vue Test Utils | `mount()`, `shallowMount()`, `wrapper.find()`, `trigger()` |134| **Angular** | TestBed | `TestBed.configureTestingModule()`, `fixture.detectChanges()` |135| **Node.js** | Supertest | `request(app).get("/api/...").expect(200)` |136| **Python** | pytest | `@pytest.fixture`, `monkeypatch`, parametrize |137| **Go** | testing | `t.Run()`, table-driven tests, `t.Parallel()` |138139---140141## Step 5: Verify and Improve142143After generating the tests:1441451. **Run the tests** — Execute the test suite to make sure they pass1462. **Check coverage** — Identify untested lines or branches1473. **Add missing cases** — Fill gaps in coverage, especially error paths1484. **Review test quality** — Tests should fail when the code breaks, not just when the tests break149150### Coverage Goals151152| Priority | Coverage Target |153|----------|----------------|154| Critical business logic | 90%+ |155| Utility functions | 85%+ |156| UI components | 80%+ |157| Configuration/glue code | 60%+ |158159Focus on **branch coverage** over line coverage — untested `else` and `catch` paths are where bugs hide.160161---162163## Output Checklist164165Before finishing, verify:166167- [ ] Tests follow the project's existing naming convention and file location168- [ ] All exported functions/components have at least one test169- [ ] Happy path tested for every function170- [ ] Error/failure path tested for every function that can fail171- [ ] Edge cases covered (null, empty, boundary values)172- [ ] External dependencies are mocked (no real API calls, no real DB)173- [ ] Async code is properly awaited174- [ ] Mocks are cleaned up in afterEach175- [ ] Tests are independent — can run in any order176- [ ] Test names read like documentation