# Test Gen

> Generates failing tests before implementation using test-driven development (TDD) with Vitest in TypeScript/Next.js projects. Writes unit test stubs covering happy path, edge cases, and error conditions, then confirms they fail. Use when writing tests first, starting TDD, doing red-green-refactor, verifying test coverage after refactoring, or when the implement workflow reaches the test step.

- Skill: `blackbearhq/test-gen` (Agent Skill)
- Install (CLI): `npx skillmds@latest add blackbearhq/test-gen`
- Raw SKILL.md: https://api.skillmd.com/api/skills/blackbearhq/test-gen/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Web & Frontend
- Author: blackbearhq (https://skillmd.com/u/blackbearhq)
- Updated: 2026-09-10
- Page: https://skillmd.com/skills/blackbearhq/test-gen

---

Generate tests for a TypeScript/Next.js project using Vitest following TDD (red-green-refactor).

## Workflow: Before implementation (red phase)

1. Read the requirement or issue description
2. Find existing test patterns — `glob **/*.test.ts **/*.spec.ts` — and match their style
3. Write failing tests covering happy path, edge cases, and error conditions
4. Run `npm run test:run -- --testPathPattern=<file>` — confirm all new tests **fail**
5. Report which tests fail and what behavior they expect

## Workflow: After refactoring (verify green)

1. Run `npm run test:run -- --testPathPattern=<file>` — confirm existing tests still pass
2. Add tests for any new code paths introduced during refactoring
3. Report results

## Example test structure

```typescript
import { describe, it, expect } from 'vitest'
import { calculateDiscount } from '../utils/pricing'

describe('calculateDiscount', () => {
  it('should apply percentage discount to subtotal', () => {
    expect(calculateDiscount(100, 0.2)).toBe(80)
  })

  it('should return original price when discount is zero', () => {
    expect(calculateDiscount(100, 0)).toBe(100)
  })

  it('should throw when discount exceeds 100%', () => {
    expect(() => calculateDiscount(100, 1.5)).toThrow()
  })
})
```

## Rules

- Descriptive names: `should [expected behavior] when [condition]`
- Mock external dependencies (APIs, DB), not internal logic
- One assertion per concept — keep tests focused
- Always run with `--testPathPattern=<file>`, never the full suite
- **Model escalation**: default Haiku. Escalate to Sonnet for complex domain logic (multi-step auth, transaction rollbacks, distributed state)

