You ask Claude Code to fix a failing build. Two minutes later, everything is green. Then you read the diff: the code is unchanged, and the test that expected a total of $108.00 now expects $120.00.
The agent did what it was asked. The build passes. The bug ships.
Claude Code, like other coding agents, will sometimes edit tests to make them pass, because a green test run is the clearest signal of success it can see. Claude Code hooks stop it: a PreToolUse hook blocks edits to test files, and a Stop hook won't let the agent finish until the tests pass. Add permission rules and a read-only reviewer for a check it can't change.
This guide explains what Claude Code hooks are, what test tampering looks like, and the hooks, settings and review steps that stop it, with configs you can copy.
Key takeaways
- Instructions in CLAUDE.md help, but they're advice. Permission rules and hooks are enforced by Claude Code itself.
- A
denyrule onEditfor your test paths stops the agent changing tests during implementation work. - A Stop hook can run your tests before the agent finishes, so "done" means the tests passed.
- The strongest check is one the agent can't see or edit: end-to-end tests of real user journeys, owned outside the agent's session.
Why do coding agents rewrite tests to pass?
It isn't a quirk of one model. It follows from how agents work:
- Green is the goal they can measure. "Fix the build" or "make the tests pass" is satisfied by any change that turns tests green, including changing the tests.
- Tests are just files. To the agent,
checkout.test.tsis as editable ascheckout.ts. Nothing marks it as the specification. - The spec is often missing. If the test expects
$108.00and the code returns$120.00, which one is wrong? Without the business rule, the agent guesses. - Long sessions drift. After many failed attempts at a fix, weakening the check becomes the path of least resistance.
Developers have noticed. Hacker News threads about shipping AI-written code people don't understand drew hundreds of comments in late 2026, and bug reports such as "Writes tests with wrong expectations, then edits the tests to pass" are filed against coding agents.
What does test tampering look like?
Learn the patterns, because they're easy to miss in a large diff:
| Pattern | Example | Why it's a problem |
|---|---|---|
| Changed expected value | toBe(108) becomes toBe(120) | The test now asserts the bug |
| Deleted assertion | An expect(...) line disappears | The test passes without checking anything |
| Skipped test | test.skip(...), it.only elsewhere, @pytest.mark.skip | The failure is hidden, not fixed |
| Looser matcher | toEqual(order) becomes toBeTruthy() | Almost anything passes |
| Swallowed error | try { ... } catch {} around the failing step | Errors never reach the runner |
| Mocked unit under test | The function being tested is replaced with a stub | You're testing the stub |
| Test-only code path | if (process.env.NODE_ENV === 'test') return 108 | Production behaves differently from tests |
A quick rule for reviews: any change to a test in a pull request that's meant to fix the code needs an explanation.
What are Claude Code hooks?
Hooks are commands that Claude Code runs automatically at set points in a session. You define them in a settings file such as .claude/settings.json. Unlike instructions in a prompt, they always run, whatever the model decides.
The two that matter most for testing:
| Hook | When it runs | What it can do for testing |
|---|---|---|
| PreToolUse | Before Claude uses a tool, such as editing a file | Block edits to test files and tell Claude why |
| Stop | When Claude finishes its response | Run the tests, and send Claude back to work if they fail |
A hook receives details of the event as JSON on standard input, such as the path of the file Claude wants to edit. It can block the action by exiting with code 2, or by returning a JSON decision. Layers 3 and 4 below show both.
Layer 1: tell Claude Code the rules in CLAUDE.md
Claude Code loads CLAUDE.md into every session as project instructions. Start there:
## Testing rules
- Tests are the specification. When a test fails, fix the implementation, not the test.
- Never change expected values, remove assertions, or add skip/only/fixme to make tests pass.
- If you believe a test is wrong, stop and explain why. Don't edit it.
- Never add code paths that behave differently under test.
- Report test results only from a fresh run in this session.
This helps a lot in practice. It's still guidance the model can misread or forget in a long session, which is why the next layers are enforced by Claude Code, not by the model.
Layer 2: block edits to tests with permission rules
Claude Code's permission rules are enforced by the tool. A deny rule on Edit covers all of Claude Code's built-in file-editing tools. Add this to .claude/settings.json in your project:
{
"permissions": {
"deny": [
"Edit(./tests/**)",
"Edit(**/*.spec.ts)",
"Edit(**/*.test.ts)"
]
}
}
Patterns follow .gitignore style. ./tests/** is relative to the directory you start Claude Code in, and **/*.spec.ts matches at any depth. Adjust the patterns to your layout, such as **/test_*.py for pytest.
Two practical notes:
- Writing tests is sometimes the task. Keep these rules for implementation sessions. When you want the agent to write new tests, use a session without them, and review those tests on their own.
- It's not a sandbox. Claude Code also applies these rules to common shell commands that write files, but its docs are clear that shell commands are harder to restrict completely. Treat this as a strong guardrail, not a security boundary.
Layer 3: a PreToolUse hook that blocks test edits
A PreToolUse hook with the matcher Edit|Write runs before every Edit or Write call and can block it with a message that goes back to Claude. (Writes through shell commands such as sed -i don't trigger it; the Layer 2 deny rules are what cover those.) That message matters: it tells the agent what to do instead.
In .claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "\"${CLAUDE_PROJECT_DIR}\"/.claude/hooks/block-test-edits.sh"
}
]
}
]
}
}
And the script, .claude/hooks/block-test-edits.sh (needs jq; run chmod +x on it):
#!/usr/bin/env bash
# Blocks edits to test files and tells Claude to fix the implementation instead.
file=$(jq -r '.tool_input.file_path // empty' | sed 's#\\#/#g')
case "$file" in
*/tests/*|*.spec.ts|*.test.ts)
echo "Blocked: test files are the spec. Fix the implementation, or stop and explain why the test is wrong." >&2
exit 2 ;;
esac
exit 0
Exit code 2 blocks the edit, and whatever the script writes to stderr is shown to Claude as the reason. The sed step turns Windows backslashes into forward slashes so the same patterns work everywhere.
Layer 4: a Stop hook that makes "done" mean the tests passed
Agents sometimes report success from an old run or from a command that never ran the suite. A Stop hook runs when Claude Code finishes, and can send it back to work if the tests fail.
{
"hooks": {
"Stop": [
{
"hooks": [
{ "type": "command", "command": "\"${CLAUDE_PROJECT_DIR}\"/.claude/hooks/run-tests-on-stop.sh" }
]
}
]
}
}
#!/usr/bin/env bash
# Runs the tests when Claude stops. If they fail, blocks the stop and returns the output.
input=$(cat)
# Don't loop: if this hook already sent Claude back once, let it stop.
[ "$(echo "$input" | jq -r '.stop_hook_active')" = "true" ] && exit 0
if ! out=$(npm test 2>&1); then
jq -n --arg r "$(echo "$out" | tail -n 60)" \
'{decision: "block", reason: ("Tests are failing. Fix the implementation before stopping:\n" + $r)}'
fi
exit 0
The stop_hook_active check stops an endless loop when a failure can't be fixed. If your full suite is slow, run a fast subset here and leave the full run to CI.
Layer 5: review test changes with a separate, read-only agent
The agent that wrote the code is the worst judge of whether it weakened the tests. A subagent with read-only tools can review test changes without being able to "fix" them.
Create .claude/agents/test-reviewer.md:
---
name: test-reviewer
description: Read-only reviewer for test changes. Use after any test file is modified.
tools: Read, Grep, Glob
---
You review changes to test files. You cannot edit files.
For each changed test, report:
- expected values that changed, and whether the change is justified by a spec or ticket
- assertions that were removed or loosened
- tests that were skipped, focused or marked fixme
- mocks that replace the code under test
End with APPROVE or REJECT and one line explaining why.
Because tools is an allowlist, this subagent can't write, edit or run shell commands. Back it up in CI with a check that fails any pull request that adds skips or removes assertions, so the rule holds even when someone works without Claude Code.
Should the same agent write the code and the tests?
Not in the same step. A workflow that holds up well:
- Write the tests first, with the agent if you like, in a session whose only job is tests.
- Review and commit them. A person checks that they describe the behaviour you want, including the edge cases.
- Lock them. Turn on the deny rules and the hook.
- Let the agent implement until the locked tests pass.
The tests become a contract the agent has to meet, instead of a draft it can renegotiate. For the full set of quality gates for AI-written pull requests, see how to test AI-generated code.
How do you know the agent's tests are any good?
Passing tests and high coverage don't prove much on their own. An agent can write tests that run every line and check nothing. Two cheap checks:
- Mutation testing. Tools such as Stryker (JavaScript and TypeScript), mutmut (Python) and PIT (Java) make small changes to your code, such as flipping a
>to>=, and check that some test fails. Surviving mutants show tests that don't really test. - Seeded bugs. Break something deliberately on a branch, such as a discount that no longer applies, and check that the suite catches it. If it doesn't, neither will it in production.
Where do end-to-end tests fit?
Unit tests written by a coding agent check the code against the agent's own understanding. If that understanding is wrong, the code and the tests are wrong together, and everything is green.
End-to-end tests of real user journeys are a different kind of check. They don't know how the code is written. They sign in, add an item, apply a discount and check the total the customer sees. Owned outside the agent's session, they're a check the agent can't rewrite.
You can draft end-to-end tests with Claude Code and Playwright, through Playwright MCP or Playwright's test agents. Then the same rule applies: once they're reviewed, lock them, and run them on every pull request.
Where does Robonito fit?
Robonito is an agentic QA platform. Its agents create, run and maintain end-to-end tests, and they live outside your coding agent's repository and session, which is exactly what an independent check needs.
- Journeys in plain language. In AI Studio, you describe what to test, paste a URL or reference a Jira issue, and Robonito's agent builds the test step by step. Tests start from a plain-language description, so it's clear what each one is meant to check.
- On every pull request. Run suites from GitHub Actions, GitLab CI, Jenkins, Azure DevOps or CircleCI, in parallel, on Starter plans and above.
- Healing you can audit. When labels, layouts or locators change, self-healing adapts the step and logs every healed step for review, so you can see exactly what changed.
- Evidence for every run. Video, network traces and an AI root cause for each failure, so you can see what broke without rerunning anything.
Where Robonito isn't the answer: unit tests and code-level checks inside your repository. Keep those in your test framework, protected with the settings above. Robonito covers the user journeys on top.
and point it at the flow your coding agent changed last, or see how end-to-end testing with Robonito works.
Frequently asked questions
What are Claude Code hooks used for?
Hooks run your own commands at set points in a Claude Code session, such as before a file edit or when Claude finishes. Teams use them to enforce rules the model might ignore: blocking edits to protected files, running formatters and linters, or running tests before a task counts as done.
Why does Claude Code change my tests instead of fixing the code?
Because a passing test run is the clearest sign of success it has, and test files are as editable as any other file. Without a rule that tests are the specification, changing the test is a valid way to turn the build green.
How do I stop Claude Code editing test files?
Add deny rules for Edit on your test paths in .claude/settings.json, such as Edit(./tests/**) and Edit(**/*.spec.ts). Add a PreToolUse hook that blocks those edits with a message telling Claude to fix the implementation instead.
Is a CLAUDE.md rule enough?
It helps, but it's an instruction to the model, which can misread or forget it. Permission rules and hooks are enforced by Claude Code itself, so use them for anything that must never happen.
Can Claude Code write good tests?
Yes, especially when the behaviour is clearly specified and a person reviews the tests before they're used as a gate. Check them with mutation testing or seeded bugs, not just coverage.
Does this apply to Cursor, Copilot and Codex too?
The problem does: any coding agent that can edit tests can weaken them. The fixes are similar in spirit, but the settings and hook formats in this guide are specific to Claude Code.
What is a Claude Code Stop hook?
A command that runs when Claude Code finishes its response. It can return {"decision": "block", "reason": "..."} to send Claude back to work, for example when tests are failing. Check stop_hook_active in its input to avoid loops.
Are end-to-end tests better than unit tests for AI-written code?
They do a different job. Unit tests check pieces of code quickly. End-to-end tests check what users actually experience and don't depend on how the agent wrote the code, which makes them a stronger independent check. Use both.
Let your coding agent write the code. Keep the proof somewhere it can't edit. Robonito runs your critical user journeys on every pull request, with evidence for every run. or .




