Stop paying for passing tests: a Claude Code hook that cut test output from 488 lines to 40
Claude Code reads everything your test runner prints. A typical npm test on a mid-size repo dumps hundreds of lines of passing tests, progress dots and coverage tables into the context window, and you pay for every one o
Claude Code reads everything your test runner prints. A typical npm test on a mid-size repo dumps hundreds of lines of passing tests, progress dots and coverage tables into the context window, and you pay for every one of them on every run.
The fix is small: a PreToolUse hook that rewrites the test command so Claude only sees the part that matters. On one of my repos that took a test run from 488 lines to 40, with the failing test still fully visible and the exit code unchanged.
How it works
Claude Code hooks can return updatedInput from a PreToolUse hook. That replaces the tool's input before it runs. The official cost docs use exactly this pattern to filter test output, so this isn't a hack, it's a documented feature.
The flow:
- Claude decides to run
npm test. - The hook sees a Bash call, checks the command is a plain test runner, and rewrites it to
run-quiet.sh npm test. -
run-quiet.shruns the real command, saves the full log to.claude/logs/last-test.log, and prints only failures (with context) plus the last 30 lines. - Claude gets a short, useful result. If it needs more, the full log is one
Readaway.
A minimal version
.claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [{ "type": "command", "command": ".claude/hooks/quiet-tests.sh" }]
}
]
}
}
.claude/hooks/quiet-tests.sh (sketch, needs jq):
#!/usr/bin/env bash
input=$(cat)
cmd=$(jq -r '.tool_input.command // ""' <<<"$input")
# only plain test runs; leave pipes, redirects, chains alone
case "$cmd" in *'|'*|*';'*|*'&'*|*'>'*|*'<'*|*'$('*) exit 0 ;; esac
grep -Eq '^(npm|pnpm|yarn) (run )?test( |$)|^(npx )?(jest|vitest)( |$)|^pytest( |$)' <<<"$cmd" || exit 0
jq -n --arg c ".claude/hooks/run-quiet.sh $cmd" \
'{hookSpecificOutput:{hookEventName:"PreToolUse",updatedInput:{command:$c}}}'
run-quiet.sh just runs "$@", tees to the log, and if the output is long, prints grep -n -A5 -iE 'fail|error' plus tail -30. Keep the original exit code (${PIPESTATUS[0]}) so Claude still knows the run failed.
Things that bit me
-
Skip compound commands. Rewriting
npm test | grep foobreaks the user's intent. Bail out on pipes, chains and redirects. - Short output should pass through untouched. If the run is under ~120 lines, filtering just hides context.
-
Permission rules match the rewritten command. If you allowlist
Bash(npm test), the rewritten command won't match. Either allow the wrapper too, or returnpermissionDecision: "allow"from the hook (make that opt-in). -
Verify it actually fires. Run
/hooksto see it registered, thenclaude --debug-file ./claude-debug.txtand look for "modified tool input keys".
Where this fits
This is one of ten skills in a free and paid "skill tree" I put together for spending Claude Code tokens on the work, not the noise: lean context (context-audit, claude-md-slim, session-handoff), fast loops (this filter, model routing for subagents, a plan gate) and agent leverage (a read-only verifier subagent, team lead patterns, a weekly /usage review).
- Tier I is free and MIT: https://github.com/geraltbhai/claude-code-skill-tree
- The full tree (10 skills, with the complete hook scripts) is $29: https://samyakvaghela.gumroad.com/l/claude-code-skill-tree
- A smaller $9 kit with 7 copy-paste prompts: https://samyakvaghela.gumroad.com/l/claude-code-playbook
Every claim in them was checked against the current Claude Code docs. If you try the hook, I'd like to hear your before/after line counts.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.