Manuals / AI Coding with Cursor / Chapter 6

B · Review & Test · intermediate · ~35 min · Chapter 6 of 11

Test-driven prompting

Write tests first, then ask the agent to make them pass. TDD + AI = fewer hallucinated implementations.

Path progress
50%

Step 1 of 2

Test-first workflow

1) Write failing test. 2) Brief agent: "Make this test pass. Do not change the test." 3) Review. 4) Refactor.

Test-first workflowDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Test-first workflodrag · tap →Try thisdrag · tap →Follow the dashed drag · tap →

Example

// 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.

Was this step clear?
Chapter learning outcomes
  • TDD with agents
  • Test-first briefs
  • Red-green-refactor

Clear these before you leave

Side quest

Page object TDD

Write a Playwright test first. Agent builds the page object.