# Test Runner Hook Automatically run relevant tests when code changes are detected, with intelligent test selection and parallel execution --- ## Metadata **Title:** Test Runner Hook **Category:** hooks **Author:** JSONbored **Added:** September 2025 **Tags:** testing, automation, ci-cd, watch, parallel **URL:** https://claudepro.directory/hooks/test-runner-hook ## Overview Automatically run relevant tests when code changes are detected, with intelligent test selection and parallel execution ## Content KEY FEATURES ? Intelligent test selection based on code changes ? Parallel test execution for faster feedback ? Support for multiple testing frameworks ? Fail-fast mode for quick feedback ? Smart retry for flaky tests ? Impact analysis and dependency mapping ? Integration with CI/CD pipelines INSTALLATION CLAUDE CODE: 1) Create hooks directory: mkdir -p .claude/hooks 2) Create hook file: touch .claude/hooks/test-runner-hook.sh 3) Make executable: chmod +x .claude/hooks/test-runner-hook.sh 4) Add configuration from Hook Configuration section above to .claude/settings.json or ~/.claude/settings.json 5) Alternative: Use the interactive /hooks command in Claude Code Requirements: ? Claude Code CLI installed ? Project directory initialized CONFIGURATION Hook Configuration: Hook Type: postToolUse Script: ./.claude/hooks/code-test-runner-hook.sh Matchers: write, edit, multiedit HOOK SCRIPT #!/usr/bin/env bash READ THE TOOL INPUT FROM STDIN INPUT=$(cat) TOOLNAME=$(echo "$INPUT" | jq -r '.toolname') FILEPATH=$(echo "$INPUT" | jq -r '.toolinput.filepath // .toolinput.path // ""') if [ -z "$FILE_PATH" ]; then exit 0 fi echo "🧪 Running tests for $FILE_PATH..." GET FILE EXTENSION AND DIRECTORY EXT="${FILE_PATH##*.}" DIR=$(dirname "$FILE_PATH") FIND AND RUN RELEVANT TESTS BASED ON FILE TYPE case "$EXT" in js|jsx|ts|tsx) # JavaScript/TypeScript files if [ -f "package.json" ]; then if command -v npm &> /dev/null && npm list jest &> /dev/null; then echo "Running Jest tests..." npm test -- --testPathPattern="$FILE_PATH" --passWithNoTests 2>/dev/null elif command -v npm &> /dev/null && npm list vitest &> /dev/null; then echo "Running Vitest tests..." npx vitest run "$FILE_PATH" 2>/dev/null fi fi ;; py) # Python files if command -v pytest &> /dev/null; then echo "Running pytest..." pytest "${FILEPATH%.}test.py" "${DIR}/test_.py" 2>/dev/null || echo "No Python tests found" elif command -v python &> /dev/null; then echo "Running Python unittest..." python -m unittest discover -s "$DIR" -p "test.py" 2>/dev/null || echo "No Python tests found" fi ;; go) # Go files if command -v go &> /dev/null; then echo "Running Go tests..." go test "${DIR}/..." 2>/dev/null || echo "No Go tests found" fi ;; java) # Java files if command -v mvn &> /dev/null && [ -f "pom.xml" ]; then echo "Running Maven tests..." mvn test 2>/dev/null elif command -v gradle &> /dev/null && [ -f "build.gradle" ]; then echo "Running Gradle tests..." gradle test 2>/dev/null fi ;; esac echo "✅ Test execution completed for $FILE_PATH" >&2 exit 0 USE CASES ? Automated testing in CI/CD pipelines ? Real-time test feedback during development ? Intelligent test selection for large codebases ? Parallel test execution for faster builds ? Pre-commit test validation TROUBLESHOOTING 1) Tests run on every file save slowing down Solution: Add file extension filter or test file detection: if [[ "$FILEPATH" == test ]] || [[ "$FILEPATH" == spec ]]; then exit 0; fi to skip running tests when editing test files themselves. 2) Jest testPathPattern not finding related tests Solution: Pattern matches test file paths not source. Use --findRelatedTests instead: npm test -- --findRelatedTests="$FILE_PATH" which finds tests importing the changed file through dependency graph. 3) Hook runs tests twice with both Jest and Vitest Solution: Detection uses npm list jest which may find both. Add explicit priority: if npm list jest &> /dev/null; then run_jest; exit 0; elif npm list vitest ... to prevent fallthrough. 4) Python tests fail to locate test directory Solution: Hook looks for ${FILEPATH%.}test.py and test_.py. For pytest, use explicit discovery: pytest --collect-only "$DIR" 2>/dev/null | grep "test session starts" to verify test detection. 5) Go tests timeout on large module changes Solution: Add timeout flag and scope: go test -timeout 30s "${DIR}" 2>/dev/null instead of ${DIR}/... which tests all subpackages. Or use go test -short for quick tests only during development. TECHNICAL DETAILS Documentation: https://jestjs.io/docs/getting-started --- Source: Claude Pro Directory Website: https://claudepro.directory URL: https://claudepro.directory/hooks/test-runner-hook This content is optimized for Large Language Models (LLMs). For full formatting and interactive features, visit the website.