Test Generator
When to activate
- You have written a new function, component, or service and need test coverage
- An existing test suite is incomplete or missing tests for critical paths
- You're refactoring code and need to ensure parity with prior behavior via tests
- You need to verify edge cases and error handling paths
When NOT to use
- Tests already exist and are passing — use
/code-review to audit coverage instead
- You're writing integration tests that require external services — scaffold with this skill, then hand-configure environment setup
- The code under test has unstable APIs or incomplete contracts — define the interface first
- You're building a test harness or framework — this skill generates tests, not testing infrastructure
Instructions
Unit Tests
- Read the target function or component — understand the public interface, arguments, return types, and documented behavior
- Identify test categories: happy path, edge cases, error conditions, boundary values, type coercion
- Generate test cases organized by category, each with:
- Descriptive test name (what behavior is being verified)
- Setup (mocks, fixtures, state)
- Assertion (what should be true)
- Write in the project's test framework (Jest, Pytest, Vitest, unittest, etc.)
- Use realistic test data — not lorem ipsum or fake names, but data shapes that match production
Component Tests
For UI components (React, Vue, Svelte):
- Test rendering — does it render without crashing?
- Test props — does each prop affect output correctly?
- Test interaction — clicks, form submission, keyboard navigation
- Test slots/children — if applicable, how does child content render?
- Test accessibility — ARIA labels, semantic HTML, keyboard focus
API Endpoint Tests
- Test each HTTP method — GET, POST, PUT, DELETE (if applicable)
- Test request validation — invalid schemas should fail gracefully
- Test response shape — does output match the documented contract?
- Test status codes — 200, 400, 401, 404, 500 as appropriate
- Test side effects — if the endpoint modifies data, verify the mutation
Coverage Focus
Prioritize tests for:
- Critical paths — happy path, must not break
- Error handling — what happens when input is malformed or service fails?
- Boundary conditions — empty arrays, null values, max/min values
- Authentication/authorization — if applicable
- Data transformations — input → output mapping
Defer or omit tests for:
- Obvious pass-throughs (e.g., a wrapper that just calls another function)
- Third-party library behavior (don't test the library, mock it)
- Trivial getters/setters with no logic
Example
Target code (JavaScript function):
export function parseQueryString(queryString) {
if (!queryString) return {};
return Object.fromEntries(
queryString
.split('&')
.map(pair => pair.split('='))
.map(([key, value]) => [
decodeURIComponent(key),
decodeURIComponent(value || '')
])
);
}
Generated test suite (Jest):
import { parseQueryString } from './query.js';
describe('parseQueryString', () => {
describe('happy path', () => {
it('parses single query parameter', () => {
expect(parseQueryString('name=Alice')).toEqual({ name: 'Alice' });
});
it('parses multiple query parameters', () => {
expect(parseQueryString('name=Alice&age=30&city=NYC')).toEqual({
name: 'Alice',
age: '30',
city: 'NYC'
});
});
});
describe('edge cases', () => {
it('returns empty object for null', () => {
expect(parseQueryString(null)).toEqual({});
});
it('returns empty object for undefined', () => {
expect(parseQueryString(undefined)).toEqual({});
});
it('returns empty object for empty string', () => {
expect(parseQueryString('')).toEqual({});
});
it('handles parameters with no value', () => {
expect(parseQueryString('flag')).toEqual({ flag: '' });
});
it('handles URL-encoded characters', () => {
expect(parseQueryString('email=user%40example.com')).toEqual({
email: 'user@example.com'
});
});
it('handles duplicate keys (last wins)', () => {
expect(parseQueryString('id=1&id=2')).toEqual({ id: '2' });
});
});
describe('error handling', () => {
it('ignores trailing ampersand', () => {
expect(parseQueryString('a=1&')).toEqual({ a: '1' });
});
it('ignores empty pairs between ampersands', () => {
expect(parseQueryString('a=1&&b=2')).toEqual({ a: '1', b: '' });
});
});
});
Why this approach works:
- Tests are independent (no shared state)
- Each test name describes the behavior, not the implementation
- Edge cases are explicit and grouped
- Error conditions are covered
- The suite can run in any order
1---2name: test-generator-23description: Test Generator4---5# Test Generator67## When to activate89- You have written a new function, component, or service and need test coverage10- An existing test suite is incomplete or missing tests for critical paths11- You're refactoring code and need to ensure parity with prior behavior via tests12- You need to verify edge cases and error handling paths1314## When NOT to use1516- Tests already exist and are passing — use `/code-review` to audit coverage instead17- You're writing integration tests that require external services — scaffold with this skill, then hand-configure environment setup18- The code under test has unstable APIs or incomplete contracts — define the interface first19- You're building a test harness or framework — this skill generates tests, not testing infrastructure2021## Instructions2223### Unit Tests24251. **Read the target function or component** — understand the public interface, arguments, return types, and documented behavior262. **Identify test categories**: happy path, edge cases, error conditions, boundary values, type coercion273. **Generate test cases** organized by category, each with:28 - Descriptive test name (what behavior is being verified)29 - Setup (mocks, fixtures, state)30 - Assertion (what should be true)314. **Write in the project's test framework** (Jest, Pytest, Vitest, unittest, etc.)325. **Use realistic test data** — not lorem ipsum or fake names, but data shapes that match production3334### Component Tests3536For UI components (React, Vue, Svelte):37381. **Test rendering** — does it render without crashing?392. **Test props** — does each prop affect output correctly?403. **Test interaction** — clicks, form submission, keyboard navigation414. **Test slots/children** — if applicable, how does child content render?425. **Test accessibility** — ARIA labels, semantic HTML, keyboard focus4344### API Endpoint Tests45461. **Test each HTTP method** — GET, POST, PUT, DELETE (if applicable)472. **Test request validation** — invalid schemas should fail gracefully483. **Test response shape** — does output match the documented contract?494. **Test status codes** — 200, 400, 401, 404, 500 as appropriate505. **Test side effects** — if the endpoint modifies data, verify the mutation5152### Coverage Focus5354Prioritize tests for:55- **Critical paths** — happy path, must not break56- **Error handling** — what happens when input is malformed or service fails?57- **Boundary conditions** — empty arrays, null values, max/min values58- **Authentication/authorization** — if applicable59- **Data transformations** — input → output mapping6061Defer or omit tests for:62- Obvious pass-throughs (e.g., a wrapper that just calls another function)63- Third-party library behavior (don't test the library, mock it)64- Trivial getters/setters with no logic6566## Example6768**Target code** (JavaScript function):6970```javascript71export function parseQueryString(queryString) {72 if (!queryString) return {};73 return Object.fromEntries(74 queryString75 .split('&')76 .map(pair => pair.split('='))77 .map(([key, value]) => [78 decodeURIComponent(key),79 decodeURIComponent(value || '')80 ])81 );82}83```8485**Generated test suite** (Jest):8687```javascript88import { parseQueryString } from './query.js';8990describe('parseQueryString', () => {91 describe('happy path', () => {92 it('parses single query parameter', () => {93 expect(parseQueryString('name=Alice')).toEqual({ name: 'Alice' });94 });9596 it('parses multiple query parameters', () => {97 expect(parseQueryString('name=Alice&age=30&city=NYC')).toEqual({98 name: 'Alice',99 age: '30',100 city: 'NYC'101 });102 });103 });104105 describe('edge cases', () => {106 it('returns empty object for null', () => {107 expect(parseQueryString(null)).toEqual({});108 });109110 it('returns empty object for undefined', () => {111 expect(parseQueryString(undefined)).toEqual({});112 });113114 it('returns empty object for empty string', () => {115 expect(parseQueryString('')).toEqual({});116 });117118 it('handles parameters with no value', () => {119 expect(parseQueryString('flag')).toEqual({ flag: '' });120 });121122 it('handles URL-encoded characters', () => {123 expect(parseQueryString('email=user%40example.com')).toEqual({124 email: 'user@example.com'125 });126 });127128 it('handles duplicate keys (last wins)', () => {129 expect(parseQueryString('id=1&id=2')).toEqual({ id: '2' });130 });131 });132133 describe('error handling', () => {134 it('ignores trailing ampersand', () => {135 expect(parseQueryString('a=1&')).toEqual({ a: '1' });136 });137138 it('ignores empty pairs between ampersands', () => {139 expect(parseQueryString('a=1&&b=2')).toEqual({ a: '1', b: '' });140 });141 });142});143```144145**Why this approach works:**146- Tests are independent (no shared state)147- Each test name describes the behavior, not the implementation148- Edge cases are explicit and grouped149- Error conditions are covered150- The suite can run in any order