B · Review & TestIntermediate35 min read
6. Test-driven prompting
AI Coding with Cursor · 44 pages source format
Write tests first, then ask the agent to make them pass. TDD + AI = fewer hallucinated implementations.
What you'll learn
- TDD with agents
- Test-first briefs
- Red-green-refactor
Test-first workflow
1) Write failing test. 2) Brief agent: "Make this test pass. Do not change the test." 3) Review. 4) Refactor.
// You write this test first:
import { describe, it, expect } from 'vitest'
import { formatCurrency } from './format'
describe('formatCurrency', () => {
it('formats USD with 2 decimals', () => {
expect(formatCurrency(1234.5, 'USD')).toBe('$1,234.50')
})
it('handles zero', () => {
expect(formatCurrency(0, 'USD')).toBe('$0.00')
})
})
// Then brief agent: "Implement formatCurrency to pass these tests."Do this now
Write a failing test for a utility function. Agent makes it pass.
Clear?
Guard the test file
Tell agent explicitly: "Do not modify test files." Prevents cheated passes.
Do this now
Run test-first workflow on 2 functions.
Clear?