Last active
February 24, 2026 03:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "hooks": { | |
| "PostToolUse": [ | |
| { | |
| "matcher": "Edit|Write", | |
| "hooks": [ | |
| { | |
| "type": "command", | |
| "command": "\"${CLAUDE_PROJECT_DIR}/.claude/hooks/test-file\"" | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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