Skip to content

Instantly share code, notes, and snippets.

@jbenner-radham
Last active February 24, 2026 03:19
Show Gist options
  • Select an option

  • Save jbenner-radham/c203cdecfdb592583ef0290126a1b041 to your computer and use it in GitHub Desktop.

Select an option

Save jbenner-radham/c203cdecfdb592583ef0290126a1b041 to your computer and use it in GitHub Desktop.
A Claude Code hook to test JS files after an edit or write operation.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "\"${CLAUDE_PROJECT_DIR}/.claude/hooks/test-file\""
}
]
}
]
}
}
#!/usr/bin/env bash
main() {
local -r filepath="$(jq --raw-output '.tool_input.file_path')"
if [[ "${filepath}" == *.ts ]]; then
local test_filepath="${filepath}"
if [[ "${filepath}" != *.spec.ts ]]; then
test_filepath="${filepath%.ts}"'.spec.ts'
fi
if [[ -f "${test_filepath}" ]] && ! npm test -- "${test_filepath}"; then
# The Claude Code docs specify to use exit code 2 to indicate an error.
# This will also print `stderr` to the terminal; whereas other exit
# codes will not.
exit 2
fi
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment