brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a Stop hook keeps Claude Code working until your tests pass

Claude
~/tips/tip-claude-stop-hook-tests-pass

echo '{"decision": "block", "reason": "The tests fail. Run npm test and fix them before you stop."}'

Blocks Claude from ending its turn while the test suite fails, with a guard against loops.

“Done!” with failing tests is annoying. A Stop hook runs when Claude wants to finish its turn and can send it back to work.

The script, saved as .claude/hooks/tests-pass.sh and made executable:

#!/bin/bash
INPUT=$(cat)
if [ "$(echo "$INPUT" | jq -r '.stop_hook_active')" = "true" ]; then
  exit 0
fi
if ! npm test --silent >/dev/null 2>&1; then
  echo '{"decision": "block", "reason": "The tests fail. Run npm test and fix them before you stop."}'
fi

And in .claude/settings.json:

{ "hooks": { "Stop": [ { "hooks": [ { "type": "command", "command": ".claude/hooks/tests-pass.sh" } ] } ] } }

When the tests fail, the hook prints decision: block with a reason. Claude doesn’t stop, it gets the reason as feedback and keeps working.

The stop_hook_active check matters. It is true when Claude is already continuing because of a Stop hook, and exiting there lets Claude stop after one extra round instead of looping. Claude Code also overrides a Stop hook that blocks eight times in a row without a tool call in between.

A Stop hook runs at the end of every turn, also when you only asked a question. A slow test suite then makes every answer slow, so a fast subset of the tests is the better choice here, in my view.

Further reading: Automate actions with hooks, Hooks reference

More Claude tips /recap /focus /skill-doctor /autocompact 500k claude purge ~/work/my-repo --dry-run All Claude tips (83) All topics

Link