Generate tests for a TypeScript/Next.js project using Vitest following TDD (red-green-refactor).
Workflow: Before implementation (red phase)
- Read the requirement or issue description
- Find existing test patterns —
glob **/*.test.ts **/*.spec.ts— and match their style - Write failing tests covering happy path, edge cases, and error conditions
- Run
npm run test:run -- --testPathPattern=<file>— confirm all new tests fail - Report which tests fail and what behavior they expect
Workflow: After refactoring (verify green)
- Run
npm run test:run -- --testPathPattern=<file>— confirm existing tests still pass - Add tests for any new code paths introduced during refactoring
- Report results
Example test structure
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)