headroom.walls.sh · test
Claude Code can write tests, run them, read the failures, fix the code, and repeat — unattended. The test-fix loop is one of its strongest workflows. This page covers how to drive TDD, add coverage to existing code, and keep long test sessions from quietly draining your 5-hour window.
Give Claude Code a failing test (or ask it to write one), and it will enter a loop: write → run → read output → fix → run again. This is the workflow it was built for.
claude "write tests for src/auth.js, run them, and fix any failures"
What happens:
src/auth.js and any existing test filesThe loop is automatic — you don't need to supervise each cycle. Start it, check back when it's done.
Claude Code handles strict TDD well. Ask for the test before the implementation:
claude "I want to add a validateEmail function to src/utils.js. Write a failing test for it first — it should validate format, reject TLDs shorter than 2 chars, and accept subdomains. Don't implement it yet."
Once you've reviewed the test spec:
claude "now implement validateEmail to make those tests pass"
This keeps the test as the contract. Claude Code reads the failing test to understand what's expected, then writes code to satisfy it exactly — not the other way around.
The most common request: a module with no tests. Give Claude Code the module and a coverage goal:
claude "add unit tests for src/parser.js — cover the happy path, malformed input, and edge cases like empty strings and null. Use the existing test framework in package.json."
It will:
src/parser.js to understand what to testpackage.json for the test runner (Jest, Vitest, Mocha, etc.)If you want a specific coverage target:
claude "add tests for src/parser.js. Run coverage after — I want 80%+ line coverage."
For a directory with no tests at all:
claude "write tests for everything in src/models/ — one test file per module, placed in test/models/. Match the naming convention test-{module}.js. Run the suite after writing."
Claude Code reads each file, infers the public API, writes tests, and runs the suite. Failures in one file don't stop it from writing the others — it logs failures and continues across the directory.
claude "add Jest tests for src/api/users.js — mock the database with jest.mock, test each exported function, run with npm test"
Claude Code understands jest.mock(), beforeEach/afterEach, snapshot testing, and async patterns. Ask it to match your project's existing test style and it will read the existing files before writing a single line.
claude "write pytest tests for app/services/auth.py — use fixtures, parametrize the edge cases, and run pytest -v after"
It understands fixtures, parametrize, monkeypatching, and the difference between unit and integration tests. Passing -v in the run command gives readable failure output Claude Code can act on.
claude "add table-driven tests for pkg/parser/parser.go — follow the existing patterns in the repo, run go test ./pkg/parser/..."
Go's table-driven test pattern is idiomatic and Claude Code follows it correctly. Giving it the run command with the right package path avoids the "tests not found" confusion from running in the wrong directory.
claude "write XCTest cases for Sources/Auth/TokenManager.swift — cover the happy path and the error cases, add them to the existing test target"
For Swift packages, Claude Code will add tests to the correct Tests/ directory and wire them into the package's test target — it reads the package manifest before writing.
If you're running tests manually and want Claude Code to fix the failures:
npm test 2>&1 | claude --print "read this test output and fix any failing tests"
The --print flag makes Claude Code non-interactive — it reads the piped output, fixes what it can, and exits. Useful for scripted pipelines, CI integration, and post-test hooks.
Or pass the output inline:
claude "here is my test output — find what is failing and fix it: [paste failure output here]"
When you rename or restructure code that has tests, tell Claude Code about both:
claude "rename getUserById to fetchUser across the codebase. Update the implementation, all call sites, and all tests. Run the test suite after to confirm nothing broke."
The critical addition is "run the test suite after" — it verifies the rename was consistent rather than assuming the edits were correct.
The test-fix loop burns session budget silently. Headroom shows your Claude Code session utilization (5h window) and weekly utilization (7d cap) live in the macOS menu bar — updated as the loop runs. No token, no API key: it reads the file Claude Code writes to ~/.claude/.
Install in one line:
Color-coded from calm to amber to red as your window fills. Start a long test run when you're at 20% — not when you're at 70% and two cycles from a hard stop.
→ Debugging with Claude Code
→ Refactoring with Claude Code
→ Agent mode and subagents
→ 5-hour session limit explained · 7-day weekly cap