Writer’s Brief

The Hook

Your production site has thousands of lines of logic and zero tests. Here’s how to add 52 tests in 30 minutes without touching the scary parts.

Main Points to Cover

  1. The trick: extract pure functions from API handlers into a shared lib/, test those. Don’t try to test the whole handler.
  2. For browser scripts (auth), mirror the logic in a testable module instead of modifying the original. Same code, separate file, zero risk.
  3. The whitelist .gitignore trap - when your project ignores everything by default, new test files are silently invisible to git.

The Angle

Practical “first tests” story. Not about TDD philosophy. About going from 0 to 52 passing tests on a real production site that’s already serving customers.

Target Reader

Solo devs or small teams with a Cloudflare Pages (or similar) project that grew organically and now has real users but no tests. They know they “should” add tests but it feels like a huge project.

Tone Notes

Casual, practical. Lean into “this is easier than you think” without being preachy about testing. The point is: you can test the logic that actually breaks without setting up a whole testing infrastructure for your entire app.

Raw Material / Moments to Write From

  • The C4C site had ~2,300 lines of business logic across 10 API handlers + a 1,035-line auth script. Zero tests. Serving paying customers.
  • The key insight: you don’t test the handler, you test the logic inside it. Pull out validateEmail, parseTimeWindow, splitName etc. as pure functions. The handler just calls them.
  • auth.js is a browser script with DOM manipulation baked in. Instead of trying to test it (jsdom, etc.), just mirror the pure logic (entitlement checking, URL parsing) in a separate file. Same code, testable context.
  • The .gitignore used * with a whitelist of allowed files. New files (package.json, lib/, tests/) were silently ignored by git. git add just failed with no explanation until you read the ignore pattern. Classic gotcha.
  • 52 tests, 297ms. That’s the punchline. Not a weekend project. Not a “testing initiative.” Just pull the logic out and test it.
  • The API handlers got slightly cleaner as a side effect - shared validation instead of copy-pasted regex in each file.