CClaude Code Catalog
All Hooks

Post-Edit Test Runner

Post-ToolBeginnerHook Type: post-tool-use

Post-Edit Test Runner triggers after every file write or edit operation. It detects which test files correspond to the modified source file using common naming conventions (*.test.ts, *.spec.ts, __tests__/*) and runs only those tests. If no matching test file exists, it skips silently. This gives you instant feedback on whether an edit broke existing functionality, catching regressions before they compound.

testingjestvitestregressionautomation

Hook Code

#!/bin/bash # Post-Edit Test Runner Hook # Runs related tests after file edits # Only trigger on file write/edit tools if [[ "$TOOL_NAME" != "Edit" ]] && [[ "$TOOL_NAME" != "Write" ]]; then exit 0 fi FILE_PATH=$(echo "$TOOL_INPUT" | jq -r '.file_path // .path // empty') if [ -z "$FILE_PATH" ]; then exit 0 fi # Skip non-source files if ! echo "$FILE_PATH" | grep -qE '\.(ts|tsx|js|jsx|py)$'; then exit 0 fi # Skip test files themselves if echo "$FILE_PATH" | grep -qE '\.(test|spec)\.(ts|tsx|js|jsx)$'; then exit 0 fi BASE_NAME=$(basename "$FILE_PATH" | sed 's/\.[^.]*$//') DIR_NAME=$(dirname "$FILE_PATH") # Find matching test files TEST_FILE="" for pattern in "$DIR_NAME/$BASE_NAME.test.ts" "$DIR_NAME/$BASE_NAME.spec.ts" "$DIR_NAME/__tests__/$BASE_NAME.test.ts"; do if [ -f "$pattern" ]; then TEST_FILE="$pattern" break fi done if [ -z "$TEST_FILE" ]; then echo "No matching test file found for $FILE_PATH, skipping." exit 0 fi echo "Running tests: $TEST_FILE" npx vitest run "$TEST_FILE" --reporter=verbose 2>&1 | tail -20 exit 0

Add this hook to your Claude Code settings or .claude/settings.json to activate.

Terminal Preview

Post-Edit Test Runner

About Post-Edit Test Runner

Claude Code hooks let you run custom shell commands automatically in response to specific events during Claude's operation. Post-Edit Test Runner is a Post-Tool hook at the Beginner level that automates tasks at key moments in your development workflow, reducing manual steps and enforcing consistency across your team.

Related Hooks