Using Claude Code for Tests: It Runs the Suite

📘 Tutorials 2026-07-18 5 min read

Most developers know they should write tests. They just don'...

📜 Table of Contents

📖 Keep reading

The thing that separates Claude Code from a code-completion tool is that it runs the tests. It will write a test, execute npm test, read the failure, fix the code, and run it again — in one turn, without you pasting error messages back and forth. That loop is the actual product.

It is also where the specific failure modes live, because an agent that can make tests pass has several ways to make them pass that you did not want.

Glass vials in a dark rack, one glowing faintly
Four ways an agent games a suite: editing the test, over-mocking, unfalsifiable asserts, coverage-chasing.

Set the command first

Claude Code cannot run your test suite until it knows how. Put it in CLAUDE.md at the repo root, and be explicit about the scope:

## Testing
- Unit tests: `npm test`
- Single file: `npx jest path/to/file.test.ts`
- Type check: `npx tsc --noEmit`
- Lint: `npx eslint . --max-warnings 0`

Prefer running a single test file while iterating.
Run the full suite only before declaring work complete.

The last two lines matter more than they look. Without them the agent runs the entire suite after every small edit, which is slow on a large repo and burns through your context with output it does not need.

Make it read before it writes

Generated tests that do not match your existing style are worse than useless: they pass, they add maintenance cost, and they teach the next agent the wrong conventions.

The prompt that works:

Read three existing test files in this directory first.
Match their structure, naming, and assertion style exactly.
Then write tests for src/payments/refund.ts.

Naming it as a step, rather than hoping it infers the convention, changes the output measurably. With an explicit read step it copies your fixtures and helpers; without one it invents its own.

The TDD loop it is actually good at

Ask for the failing test first, and make the failure a precondition:

Write a test for the refund calculation that fails against the current code.
Show me the failure output. Do not modify src/ yet.

"Do not modify src/ yet" is the load-bearing clause. Without it, the agent sees a failing test and immediately fixes the implementation, which is the opposite of what you asked for — you wanted to see the test fail for the right reason before trusting it.

Once the failure is confirmed and it is failing for the correct reason, then let it implement. That two-phase split is where TDD with an agent produces better code than TDD alone, because the failure output is evidence rather than an assumption.

The failure modes

It edits the test to make it pass. The most common and most damaging. The agent hits a failing assertion, decides the assertion was wrong, and changes the expected value. You get green tests and unchanged behaviour.

Countermeasure: review every diff that touches a test file with the same suspicion you apply to production code, and never accept a passing run where the test file changed in the same turn as the source file without reading both.

It mocks the thing under test. Asked to test a function that calls a database, it may mock the database client so thoroughly that the test asserts nothing about the function's logic.

Countermeasure: state the boundary explicitly — "mock only the HTTP client, use a real in-memory database" — and check the mock count in the diff.

It writes tests that cannot fail. Assertions on truthy values, expect(result).toBeDefined(), or a snapshot generated from the current (wrong) behaviour. These all pass and all catch nothing.

Countermeasure: after the agent says done, revert one line of the implementation and re-run. If the suite still passes, the tests are decoration.

It optimises for coverage. Coverage is a number the agent can move without improving anything. Asking for "90% coverage" gets you 90% of lines executed, not 90% of behaviour verified.

Countermeasure: ask for specific behaviours, not a percentage. "Test that a refund over the original amount is rejected" is checkable; "raise coverage to 90%" is gameable.

What it is genuinely faster at

Reading unfamiliar code to find what to test. Give it a module you have never seen and ask what the edge cases are. It will enumerate null inputs, empty arrays, and boundary conditions faster than you will, and the enumeration is usually a reasonable starting list.

Fixture generation. Building a realistic order object with twelve fields is tedious and mechanical. This is exactly the work worth delegating.

Reproducing a bug as a test. Paste a stack trace and ask for a failing test that reproduces it. Converting a production error into a regression test is the highest-value use of the tool, and it is the one most people skip.

A workflow that holds up

  1. CLAUDE.md documents the test commands and the single-file invocation.
  2. Ask what the edge cases are before asking for code.
  3. Write the failing test first; refuse implementation until the failure is correct.
  4. Let it implement and iterate against the suite.
  5. Read the diff. Any change to an existing test is a question, not an improvement.
  6. Revert a line of source and confirm the suite goes red.
  7. Run the full suite once, at the end.

Step 6 is the one that separates a suite you can trust from a suite that merely passes, and it takes ten seconds.

Related Articles
2026-08-05
Open Source AI Translation in 2026: LibreTranslate (16k Stars) vs Argos Translate (6k) vs Local LLMs - Free Private Translation
2026-08-11
Knowledge Graphs for AI 2026: GraphRAG, Entity Extraction and Real Use Cases
2026-08-11
MLOps Meaning Explained 2026: What Machine Learning Operations Actually Is
2026-07-22
ComfyUI Tutorial for Beginners 2026
2026-07-20
Open Source AI Hardware Requirements: Complete Guide to Building Your Local AI Rig
2026-07-16
LLM Evaluation Metrics in Python 2026: Complete Guide with RAGAS and DeepEval

Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.

💬 Comments (0)

No comments yet. Be the first!

Login to comment