Loading...
Automatically updates Jest snapshots when component files are modified significantly
{
"hookConfig": {
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/jest-snapshot-auto-updater.sh",
"matchers": [
"write",
"edit"
]
}
}
},
"scriptContent": "#!/usr/bin/env bash\n\n# Read the tool input from stdin\nINPUT=$(cat)\nTOOL_NAME=$(echo \"$INPUT\" | jq -r '.tool_name')\nFILE_PATH=$(echo \"$INPUT\" | jq -r '.tool_input.file_path // .tool_input.path // \"\"')\n\nif [ -z \"$FILE_PATH\" ]; then\n exit 0\nfi\n\n# Check if this is a component file that might have Jest snapshots\nif [[ \"$FILE_PATH\" == *.jsx ]] || [[ \"$FILE_PATH\" == *.tsx ]] || [[ \"$FILE_PATH\" == *.js ]] || [[ \"$FILE_PATH\" == *.ts ]] || [[ \"$FILE_PATH\" == *.vue ]] || [[ \"$FILE_PATH\" == *.component.ts ]]; then\n echo \"๐ธ Jest Snapshot Management for: $(basename \"$FILE_PATH\")\" >&2\n \n # Initialize counters\n SNAPSHOTS_UPDATED=0\n TESTS_RUN=0\n TESTS_PASSED=0\n TESTS_FAILED=0\n SNAPSHOT_FILES_FOUND=0\n \n # Function to report snapshot operations\n report_snapshot() {\n local level=\"$1\"\n local message=\"$2\"\n \n case \"$level\" in\n \"SUCCESS\")\n echo \"โ
SUCCESS: $message\" >&2\n ;;\n \"WARNING\")\n echo \"โ ๏ธ WARNING: $message\" >&2\n ;;\n \"ERROR\")\n echo \"โ ERROR: $message\" >&2\n ;;\n \"INFO\")\n echo \"โน๏ธ INFO: $message\" >&2\n ;;\n \"SNAPSHOT\")\n echo \"๐ธ SNAPSHOT: $message\" >&2\n SNAPSHOTS_UPDATED=$((SNAPSHOTS_UPDATED + 1))\n ;;\n esac\n }\n \n # Extract component information\n FILE_NAME=\"$(basename \"$FILE_PATH\")\"\n COMPONENT_NAME=\"${FILE_NAME%.*}\"\n FILE_DIR=\"$(dirname \"$FILE_PATH\")\"\n \n # Check if this is likely a component or test file\n if [[ \"$FILE_NAME\" == *.test.* ]] || [[ \"$FILE_NAME\" == *.spec.* ]]; then\n echo \" ๐งช Test file detected - checking for snapshot updates\" >&2\n else\n echo \" ๐ง Component file detected - looking for related tests\" >&2\n fi\n \n # Check if Jest is available\n if ! command -v npx &> /dev/null; then\n report_snapshot \"ERROR\" \"npx not available - cannot run Jest\"\n exit 1\n fi\n \n # Check if Jest is configured in the project\n JEST_CONFIG_FOUND=false\n if [ -f \"package.json\" ]; then\n if grep -q '\"jest\"' package.json 2>/dev/null || grep -q '\"@jest\"' package.json 2>/dev/null; then\n JEST_CONFIG_FOUND=true\n echo \" ๐ Jest configuration found in package.json\" >&2\n fi\n fi\n \n if [ -f \"jest.config.js\" ] || [ -f \"jest.config.ts\" ] || [ -f \"jest.config.json\" ]; then\n JEST_CONFIG_FOUND=true\n echo \" ๐ Jest configuration file found\" >&2\n fi\n \n if [ \"$JEST_CONFIG_FOUND\" = false ]; then\n report_snapshot \"WARNING\" \"No Jest configuration found - snapshots may not be available\"\n exit 0\n fi\n \n # 1. Find existing snapshot files\n echo \"๐ Searching for existing snapshot files...\" >&2\n \n SNAPSHOT_DIRS=(\"__snapshots__\" \"snapshots\" \"__tests__/__snapshots__\" \"tests/__snapshots__\")\n SNAPSHOT_FILES=()\n \n for dir in \"${SNAPSHOT_DIRS[@]}\"; do\n if [ -d \"$FILE_DIR/$dir\" ]; then\n while IFS= read -r -d '' file; do\n SNAPSHOT_FILES+=(\"$file\")\n done < <(find \"$FILE_DIR/$dir\" -name \"*.snap\" -print0 2>/dev/null)\n fi\n done\n \n # Also check for snapshots in test directories\n while IFS= read -r -d '' file; do\n SNAPSHOT_FILES+=(\"$file\")\n done < <(find . -name \"*.snap\" -path \"*$COMPONENT_NAME*\" -print0 2>/dev/null)\n \n SNAPSHOT_FILES_FOUND=${#SNAPSHOT_FILES[@]}\n \n if [ \"$SNAPSHOT_FILES_FOUND\" -gt 0 ]; then\n echo \" ๐ Found $SNAPSHOT_FILES_FOUND snapshot files related to this component\" >&2\n for snapshot in \"${SNAPSHOT_FILES[@]}\"; do\n echo \" - $(basename \"$snapshot\")\" >&2\n done\n else\n echo \" โน๏ธ No existing snapshot files found for this component\" >&2\n fi\n \n # 2. Find test files for this component\n echo \"๐งช Locating test files...\" >&2\n \n TEST_PATTERNS=(\n \"${COMPONENT_NAME}.test.*\"\n \"${COMPONENT_NAME}.spec.*\"\n \"*${COMPONENT_NAME}*.test.*\"\n \"*${COMPONENT_NAME}*.spec.*\"\n )\n \n TEST_FILES=()\n for pattern in \"${TEST_PATTERNS[@]}\"; do\n while IFS= read -r -d '' file; do\n TEST_FILES+=(\"$file\")\n done < <(find . -name \"$pattern\" -print0 2>/dev/null)\n done\n \n TEST_FILES_COUNT=${#TEST_FILES[@]}\n \n if [ \"$TEST_FILES_COUNT\" -gt 0 ]; then\n echo \" ๐ฏ Found $TEST_FILES_COUNT test files\" >&2\n for test_file in \"${TEST_FILES[@]}\"; do\n echo \" - $(basename \"$test_file\")\" >&2\n done\n else\n echo \" โ ๏ธ No test files found for component: $COMPONENT_NAME\" >&2\n report_snapshot \"INFO\" \"Consider creating tests for better component coverage\"\n fi\n \n # 3. Check if component file has been significantly modified\n echo \"๐ Analyzing component changes...\" >&2\n \n # Check git status to see if file was modified\n if command -v git &> /dev/null && git rev-parse --git-dir > /dev/null 2>&1; then\n if git status --porcelain \"$FILE_PATH\" | grep -q '^.M'; then\n echo \" ๐ Component has been modified since last commit\" >&2\n \n # Get the diff to understand the scope of changes\n LINES_CHANGED=$(git diff \"$FILE_PATH\" 2>/dev/null | grep -c '^[+-]' || echo \"0\")\n if [ \"$LINES_CHANGED\" -gt 10 ]; then\n echo \" ๐ Significant changes detected ($LINES_CHANGED lines modified)\" >&2\n SHOULD_UPDATE_SNAPSHOTS=true\n else\n echo \" ๐ Minor changes detected ($LINES_CHANGED lines modified)\" >&2\n SHOULD_UPDATE_SNAPSHOTS=false\n fi\n else\n echo \" โ
Component file is clean (no unsaved changes)\" >&2\n SHOULD_UPDATE_SNAPSHOTS=false\n fi\n else\n echo \" โน๏ธ Not in a git repository - assuming snapshots should be checked\" >&2\n SHOULD_UPDATE_SNAPSHOTS=true\n fi\n \n # 4. Run tests and update snapshots if needed\n if [ \"$TEST_FILES_COUNT\" -gt 0 ]; then\n echo \"๐งช Running tests for component...\" >&2\n \n # Determine the test command\n TEST_COMMAND=\"npm test\"\n if [ -f \"yarn.lock\" ]; then\n TEST_COMMAND=\"yarn test\"\n elif [ -f \"pnpm-lock.yaml\" ]; then\n TEST_COMMAND=\"pnpm test\"\n fi\n \n # Create test patterns for Jest\n TEST_PATTERN=\"$COMPONENT_NAME\"\n \n echo \" ๐ Running: $TEST_COMMAND -- --testNamePattern='$TEST_PATTERN' --coverage=false --watchAll=false\" >&2\n \n # Run tests without updating snapshots first\n TEST_OUTPUT_FILE=\"/tmp/jest_output_$$\"\n if $TEST_COMMAND -- --testNamePattern=\"$TEST_PATTERN\" --coverage=false --watchAll=false --verbose=false > \"$TEST_OUTPUT_FILE\" 2>&1; then\n TESTS_PASSED=$(grep -c 'PASS' \"$TEST_OUTPUT_FILE\" 2>/dev/null || echo \"0\")\n report_snapshot \"SUCCESS\" \"Tests passed ($TESTS_PASSED test suites)\"\n \n # Check if snapshots are outdated\n if grep -q 'snapshot.*failed' \"$TEST_OUTPUT_FILE\" 2>/dev/null || grep -q 'snapshot.*obsolete' \"$TEST_OUTPUT_FILE\" 2>/dev/null; then\n echo \" ๐ธ Outdated snapshots detected\" >&2\n SHOULD_UPDATE_SNAPSHOTS=true\n fi\n \n else\n TESTS_FAILED=$(grep -c 'FAIL' \"$TEST_OUTPUT_FILE\" 2>/dev/null || echo \"1\")\n echo \" โ Tests failed ($TESTS_FAILED test suites) - checking for snapshot issues\" >&2\n \n # Check if failures are due to snapshot mismatches\n if grep -q 'Snapshot.*differ' \"$TEST_OUTPUT_FILE\" 2>/dev/null; then\n echo \" ๐ธ Snapshot mismatches detected - snapshots may need updating\" >&2\n SHOULD_UPDATE_SNAPSHOTS=true\n else\n report_snapshot \"ERROR\" \"Tests failing for reasons other than snapshots\"\n echo \" ๐ Test output summary:\" >&2\n tail -10 \"$TEST_OUTPUT_FILE\" | while read line; do\n echo \" $line\" >&2\n done\n fi\n fi\n \n rm -f \"$TEST_OUTPUT_FILE\"\n fi\n \n # 5. Update snapshots if needed\n if [ \"$SHOULD_UPDATE_SNAPSHOTS\" = true ] && [ \"$TEST_FILES_COUNT\" -gt 0 ]; then\n echo \"๐ธ Updating Jest snapshots...\" >&2\n \n # Run with snapshot update flag\n UPDATE_OUTPUT_FILE=\"/tmp/jest_update_$$\"\n if $TEST_COMMAND -- --testNamePattern=\"$TEST_PATTERN\" --updateSnapshot --coverage=false --watchAll=false > \"$UPDATE_OUTPUT_FILE\" 2>&1; then\n \n # Count updated snapshots\n SNAPSHOTS_WRITTEN=$(grep -c 'snapshot.*written' \"$UPDATE_OUTPUT_FILE\" 2>/dev/null || echo \"0\")\n SNAPSHOTS_UPDATED_COUNT=$(grep -c 'snapshot.*updated' \"$UPDATE_OUTPUT_FILE\" 2>/dev/null || echo \"0\")\n \n if [ \"$SNAPSHOTS_WRITTEN\" -gt 0 ] || [ \"$SNAPSHOTS_UPDATED_COUNT\" -gt 0 ]; then\n report_snapshot \"SNAPSHOT\" \"Updated $((SNAPSHOTS_WRITTEN + SNAPSHOTS_UPDATED_COUNT)) snapshots\"\n \n # Show which snapshots were affected\n grep 'snapshot.*written\\|snapshot.*updated' \"$UPDATE_OUTPUT_FILE\" 2>/dev/null | head -5 | while read line; do\n echo \" $line\" >&2\n done\n else\n report_snapshot \"INFO\" \"Snapshot update completed - no changes needed\"\n fi\n \n else\n report_snapshot \"ERROR\" \"Failed to update snapshots\"\n echo \" ๐ Update error details:\" >&2\n tail -5 \"$UPDATE_OUTPUT_FILE\" | while read line; do\n echo \" $line\" >&2\n done\n fi\n \n rm -f \"$UPDATE_OUTPUT_FILE\"\n else\n echo \" โน๏ธ Snapshot updates not needed at this time\" >&2\n fi\n \n # 6. Clean up orphaned snapshots\n echo \"๐งน Checking for orphaned snapshots...\" >&2\n \n if [ \"$SNAPSHOT_FILES_FOUND\" -gt 0 ]; then\n # This is a simplified check - in practice, you'd want more sophisticated orphan detection\n POTENTIALLY_ORPHANED=0\n \n for snapshot_file in \"${SNAPSHOT_FILES[@]}\"; do\n SNAPSHOT_BASE=$(basename \"$snapshot_file\" .snap)\n \n # Check if there's a corresponding test or component file\n if ! find . -name \"*${SNAPSHOT_BASE%.*}*\" -type f \\( -name \"*.test.*\" -o -name \"*.spec.*\" \\) 2>/dev/null | head -1 | grep -q .; then\n POTENTIALLY_ORPHANED=$((POTENTIALLY_ORPHANED + 1))\n fi\n done\n \n if [ \"$POTENTIALLY_ORPHANED\" -gt 0 ]; then\n report_snapshot \"WARNING\" \"$POTENTIALLY_ORPHANED potentially orphaned snapshot files detected\"\n echo \" ๐ก Run 'npm test -- --updateSnapshot' to clean up unused snapshots\" >&2\n else\n echo \" โ
No orphaned snapshots detected\" >&2\n fi\n fi\n \n # 7. Generate summary report\n echo \"\" >&2\n echo \"๐ Jest Snapshot Management Summary:\" >&2\n echo \"===================================\" >&2\n echo \" ๐ Component: $COMPONENT_NAME\" >&2\n echo \" ๐งช Test files found: $TEST_FILES_COUNT\" >&2\n echo \" ๐ธ Snapshot files: $SNAPSHOT_FILES_FOUND\" >&2\n echo \" โ
Tests passed: $TESTS_PASSED\" >&2\n echo \" โ Tests failed: $TESTS_FAILED\" >&2\n echo \" ๐ธ Snapshots updated: $SNAPSHOTS_UPDATED\" >&2\n \n if [ \"$SNAPSHOTS_UPDATED\" -gt 0 ]; then\n echo \" ๐ Status: SNAPSHOTS UPDATED - Review changes before committing\" >&2\n elif [ \"$TESTS_FAILED\" -gt 0 ]; then\n echo \" โ ๏ธ Status: TESTS FAILING - Fix issues before proceeding\" >&2\n elif [ \"$TEST_FILES_COUNT\" -eq 0 ]; then\n echo \" ๐ Status: NO TESTS - Consider adding snapshot tests\" >&2\n else\n echo \" โ
Status: ALL GOOD - Snapshots are up to date\" >&2\n fi\n \n echo \"\" >&2\n echo \"๐ก Jest Snapshot Best Practices:\" >&2\n echo \" โข Review snapshot changes carefully before committing\" >&2\n echo \" โข Keep snapshots small and focused\" >&2\n echo \" โข Update snapshots only when UI changes are intentional\" >&2\n echo \" โข Use descriptive test names for better snapshot organization\" >&2\n echo \" โข Consider using 'toMatchInlineSnapshot' for small snapshots\" >&2\n echo \" โข Run 'npm test -- --updateSnapshot' to update all snapshots\" >&2\n \n # Exit with error if tests are failing for non-snapshot reasons\n if [ \"$TESTS_FAILED\" -gt 0 ] && [ \"$SNAPSHOTS_UPDATED\" -eq 0 ]; then\n echo \"โ ๏ธ Jest tests are failing - please review and fix issues\" >&2\n exit 1\n fi\n \nelse\n # Not a component file, exit silently\n exit 0\nfi\n\nexit 0"
}.claude/hooks/~/.claude/hooks/{
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/jest-snapshot-auto-updater.sh",
"matchers": [
"write",
"edit"
]
}
}
}#!/usr/bin/env bash
# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Check if this is a component file that might have Jest snapshots
if [[ "$FILE_PATH" == *.jsx ]] || [[ "$FILE_PATH" == *.tsx ]] || [[ "$FILE_PATH" == *.js ]] || [[ "$FILE_PATH" == *.ts ]] || [[ "$FILE_PATH" == *.vue ]] || [[ "$FILE_PATH" == *.component.ts ]]; then
echo "๐ธ Jest Snapshot Management for: $(basename "$FILE_PATH")" >&2
# Initialize counters
SNAPSHOTS_UPDATED=0
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
SNAPSHOT_FILES_FOUND=0
# Function to report snapshot operations
report_snapshot() {
local level="$1"
local message="$2"
case "$level" in
"SUCCESS")
echo "โ
SUCCESS: $message" >&2
;;
"WARNING")
echo "โ ๏ธ WARNING: $message" >&2
;;
"ERROR")
echo "โ ERROR: $message" >&2
;;
"INFO")
echo "โน๏ธ INFO: $message" >&2
;;
"SNAPSHOT")
echo "๐ธ SNAPSHOT: $message" >&2
SNAPSHOTS_UPDATED=$((SNAPSHOTS_UPDATED + 1))
;;
esac
}
# Extract component information
FILE_NAME="$(basename "$FILE_PATH")"
COMPONENT_NAME="${FILE_NAME%.*}"
FILE_DIR="$(dirname "$FILE_PATH")"
# Check if this is likely a component or test file
if [[ "$FILE_NAME" == *.test.* ]] || [[ "$FILE_NAME" == *.spec.* ]]; then
echo " ๐งช Test file detected - checking for snapshot updates" >&2
else
echo " ๐ง Component file detected - looking for related tests" >&2
fi
# Check if Jest is available
if ! command -v npx &> /dev/null; then
report_snapshot "ERROR" "npx not available - cannot run Jest"
exit 1
fi
# Check if Jest is configured in the project
JEST_CONFIG_FOUND=false
if [ -f "package.json" ]; then
if grep -q '"jest"' package.json 2>/dev/null || grep -q '"@jest"' package.json 2>/dev/null; then
JEST_CONFIG_FOUND=true
echo " ๐ Jest configuration found in package.json" >&2
fi
fi
if [ -f "jest.config.js" ] || [ -f "jest.config.ts" ] || [ -f "jest.config.json" ]; then
JEST_CONFIG_FOUND=true
echo " ๐ Jest configuration file found" >&2
fi
if [ "$JEST_CONFIG_FOUND" = false ]; then
report_snapshot "WARNING" "No Jest configuration found - snapshots may not be available"
exit 0
fi
# 1. Find existing snapshot files
echo "๐ Searching for existing snapshot files..." >&2
SNAPSHOT_DIRS=("__snapshots__" "snapshots" "__tests__/__snapshots__" "tests/__snapshots__")
SNAPSHOT_FILES=()
for dir in "${SNAPSHOT_DIRS[@]}"; do
if [ -d "$FILE_DIR/$dir" ]; then
while IFS= read -r -d '' file; do
SNAPSHOT_FILES+=("$file")
done < <(find "$FILE_DIR/$dir" -name "*.snap" -print0 2>/dev/null)
fi
done
# Also check for snapshots in test directories
while IFS= read -r -d '' file; do
SNAPSHOT_FILES+=("$file")
done < <(find . -name "*.snap" -path "*$COMPONENT_NAME*" -print0 2>/dev/null)
SNAPSHOT_FILES_FOUND=${#SNAPSHOT_FILES[@]}
if [ "$SNAPSHOT_FILES_FOUND" -gt 0 ]; then
echo " ๐ Found $SNAPSHOT_FILES_FOUND snapshot files related to this component" >&2
for snapshot in "${SNAPSHOT_FILES[@]}"; do
echo " - $(basename "$snapshot")" >&2
done
else
echo " โน๏ธ No existing snapshot files found for this component" >&2
fi
# 2. Find test files for this component
echo "๐งช Locating test files..." >&2
TEST_PATTERNS=(
"${COMPONENT_NAME}.test.*"
"${COMPONENT_NAME}.spec.*"
"*${COMPONENT_NAME}*.test.*"
"*${COMPONENT_NAME}*.spec.*"
)
TEST_FILES=()
for pattern in "${TEST_PATTERNS[@]}"; do
while IFS= read -r -d '' file; do
TEST_FILES+=("$file")
done < <(find . -name "$pattern" -print0 2>/dev/null)
done
TEST_FILES_COUNT=${#TEST_FILES[@]}
if [ "$TEST_FILES_COUNT" -gt 0 ]; then
echo " ๐ฏ Found $TEST_FILES_COUNT test files" >&2
for test_file in "${TEST_FILES[@]}"; do
echo " - $(basename "$test_file")" >&2
done
else
echo " โ ๏ธ No test files found for component: $COMPONENT_NAME" >&2
report_snapshot "INFO" "Consider creating tests for better component coverage"
fi
# 3. Check if component file has been significantly modified
echo "๐ Analyzing component changes..." >&2
# Check git status to see if file was modified
if command -v git &> /dev/null && git rev-parse --git-dir > /dev/null 2>&1; then
if git status --porcelain "$FILE_PATH" | grep -q '^.M'; then
echo " ๐ Component has been modified since last commit" >&2
# Get the diff to understand the scope of changes
LINES_CHANGED=$(git diff "$FILE_PATH" 2>/dev/null | grep -c '^[+-]' || echo "0")
if [ "$LINES_CHANGED" -gt 10 ]; then
echo " ๐ Significant changes detected ($LINES_CHANGED lines modified)" >&2
SHOULD_UPDATE_SNAPSHOTS=true
else
echo " ๐ Minor changes detected ($LINES_CHANGED lines modified)" >&2
SHOULD_UPDATE_SNAPSHOTS=false
fi
else
echo " โ
Component file is clean (no unsaved changes)" >&2
SHOULD_UPDATE_SNAPSHOTS=false
fi
else
echo " โน๏ธ Not in a git repository - assuming snapshots should be checked" >&2
SHOULD_UPDATE_SNAPSHOTS=true
fi
# 4. Run tests and update snapshots if needed
if [ "$TEST_FILES_COUNT" -gt 0 ]; then
echo "๐งช Running tests for component..." >&2
# Determine the test command
TEST_COMMAND="npm test"
if [ -f "yarn.lock" ]; then
TEST_COMMAND="yarn test"
elif [ -f "pnpm-lock.yaml" ]; then
TEST_COMMAND="pnpm test"
fi
# Create test patterns for Jest
TEST_PATTERN="$COMPONENT_NAME"
echo " ๐ Running: $TEST_COMMAND -- --testNamePattern='$TEST_PATTERN' --coverage=false --watchAll=false" >&2
# Run tests without updating snapshots first
TEST_OUTPUT_FILE="/tmp/jest_output_$$"
if $TEST_COMMAND -- --testNamePattern="$TEST_PATTERN" --coverage=false --watchAll=false --verbose=false > "$TEST_OUTPUT_FILE" 2>&1; then
TESTS_PASSED=$(grep -c 'PASS' "$TEST_OUTPUT_FILE" 2>/dev/null || echo "0")
report_snapshot "SUCCESS" "Tests passed ($TESTS_PASSED test suites)"
# Check if snapshots are outdated
if grep -q 'snapshot.*failed' "$TEST_OUTPUT_FILE" 2>/dev/null || grep -q 'snapshot.*obsolete' "$TEST_OUTPUT_FILE" 2>/dev/null; then
echo " ๐ธ Outdated snapshots detected" >&2
SHOULD_UPDATE_SNAPSHOTS=true
fi
else
TESTS_FAILED=$(grep -c 'FAIL' "$TEST_OUTPUT_FILE" 2>/dev/null || echo "1")
echo " โ Tests failed ($TESTS_FAILED test suites) - checking for snapshot issues" >&2
# Check if failures are due to snapshot mismatches
if grep -q 'Snapshot.*differ' "$TEST_OUTPUT_FILE" 2>/dev/null; then
echo " ๐ธ Snapshot mismatches detected - snapshots may need updating" >&2
SHOULD_UPDATE_SNAPSHOTS=true
else
report_snapshot "ERROR" "Tests failing for reasons other than snapshots"
echo " ๐ Test output summary:" >&2
tail -10 "$TEST_OUTPUT_FILE" | while read line; do
echo " $line" >&2
done
fi
fi
rm -f "$TEST_OUTPUT_FILE"
fi
# 5. Update snapshots if needed
if [ "$SHOULD_UPDATE_SNAPSHOTS" = true ] && [ "$TEST_FILES_COUNT" -gt 0 ]; then
echo "๐ธ Updating Jest snapshots..." >&2
# Run with snapshot update flag
UPDATE_OUTPUT_FILE="/tmp/jest_update_$$"
if $TEST_COMMAND -- --testNamePattern="$TEST_PATTERN" --updateSnapshot --coverage=false --watchAll=false > "$UPDATE_OUTPUT_FILE" 2>&1; then
# Count updated snapshots
SNAPSHOTS_WRITTEN=$(grep -c 'snapshot.*written' "$UPDATE_OUTPUT_FILE" 2>/dev/null || echo "0")
SNAPSHOTS_UPDATED_COUNT=$(grep -c 'snapshot.*updated' "$UPDATE_OUTPUT_FILE" 2>/dev/null || echo "0")
if [ "$SNAPSHOTS_WRITTEN" -gt 0 ] || [ "$SNAPSHOTS_UPDATED_COUNT" -gt 0 ]; then
report_snapshot "SNAPSHOT" "Updated $((SNAPSHOTS_WRITTEN + SNAPSHOTS_UPDATED_COUNT)) snapshots"
# Show which snapshots were affected
grep 'snapshot.*written\|snapshot.*updated' "$UPDATE_OUTPUT_FILE" 2>/dev/null | head -5 | while read line; do
echo " $line" >&2
done
else
report_snapshot "INFO" "Snapshot update completed - no changes needed"
fi
else
report_snapshot "ERROR" "Failed to update snapshots"
echo " ๐ Update error details:" >&2
tail -5 "$UPDATE_OUTPUT_FILE" | while read line; do
echo " $line" >&2
done
fi
rm -f "$UPDATE_OUTPUT_FILE"
else
echo " โน๏ธ Snapshot updates not needed at this time" >&2
fi
# 6. Clean up orphaned snapshots
echo "๐งน Checking for orphaned snapshots..." >&2
if [ "$SNAPSHOT_FILES_FOUND" -gt 0 ]; then
# This is a simplified check - in practice, you'd want more sophisticated orphan detection
POTENTIALLY_ORPHANED=0
for snapshot_file in "${SNAPSHOT_FILES[@]}"; do
SNAPSHOT_BASE=$(basename "$snapshot_file" .snap)
# Check if there's a corresponding test or component file
if ! find . -name "*${SNAPSHOT_BASE%.*}*" -type f \( -name "*.test.*" -o -name "*.spec.*" \) 2>/dev/null | head -1 | grep -q .; then
POTENTIALLY_ORPHANED=$((POTENTIALLY_ORPHANED + 1))
fi
done
if [ "$POTENTIALLY_ORPHANED" -gt 0 ]; then
report_snapshot "WARNING" "$POTENTIALLY_ORPHANED potentially orphaned snapshot files detected"
echo " ๐ก Run 'npm test -- --updateSnapshot' to clean up unused snapshots" >&2
else
echo " โ
No orphaned snapshots detected" >&2
fi
fi
# 7. Generate summary report
echo "" >&2
echo "๐ Jest Snapshot Management Summary:" >&2
echo "===================================" >&2
echo " ๐ Component: $COMPONENT_NAME" >&2
echo " ๐งช Test files found: $TEST_FILES_COUNT" >&2
echo " ๐ธ Snapshot files: $SNAPSHOT_FILES_FOUND" >&2
echo " โ
Tests passed: $TESTS_PASSED" >&2
echo " โ Tests failed: $TESTS_FAILED" >&2
echo " ๐ธ Snapshots updated: $SNAPSHOTS_UPDATED" >&2
if [ "$SNAPSHOTS_UPDATED" -gt 0 ]; then
echo " ๐ Status: SNAPSHOTS UPDATED - Review changes before committing" >&2
elif [ "$TESTS_FAILED" -gt 0 ]; then
echo " โ ๏ธ Status: TESTS FAILING - Fix issues before proceeding" >&2
elif [ "$TEST_FILES_COUNT" -eq 0 ]; then
echo " ๐ Status: NO TESTS - Consider adding snapshot tests" >&2
else
echo " โ
Status: ALL GOOD - Snapshots are up to date" >&2
fi
echo "" >&2
echo "๐ก Jest Snapshot Best Practices:" >&2
echo " โข Review snapshot changes carefully before committing" >&2
echo " โข Keep snapshots small and focused" >&2
echo " โข Update snapshots only when UI changes are intentional" >&2
echo " โข Use descriptive test names for better snapshot organization" >&2
echo " โข Consider using 'toMatchInlineSnapshot' for small snapshots" >&2
echo " โข Run 'npm test -- --updateSnapshot' to update all snapshots" >&2
# Exit with error if tests are failing for non-snapshot reasons
if [ "$TESTS_FAILED" -gt 0 ] && [ "$SNAPSHOTS_UPDATED" -eq 0 ]; then
echo "โ ๏ธ Jest tests are failing - please review and fix issues" >&2
exit 1
fi
else
# Not a component file, exit silently
exit 0
fi
exit 0Hook runs tests for every file change even non-test files
Detection too broad (matches all .js/.ts). Add matchers: 'matchers': ['write:**/*.{test,spec}.{js,ts,jsx,tsx}', 'edit:**/*.{test,spec}.{js,ts,jsx,tsx}'] to trigger only on test files.
Jest runs entire test suite instead of component-specific tests
testNamePattern with component name unreliable. Use --testPathPattern: '$TEST_COMMAND -- --testPathPattern="$COMPONENT_NAME" --watchAll=false'. Targets files not test descriptions.
Parallel test execution with --maxWorkers causes race conditions
No worker limit causes resource exhaustion. Add '--maxWorkers=2': '$TEST_COMMAND -- --testPathPattern="$TEST_PATTERN" --maxWorkers=2 --watchAll=false' for stable CI execution.
Snapshot update creates duplicate snapshot files after renaming
Old snapshots persist when components renamed. Run cleanup: 'npm test -- --updateSnapshot --clearCache' deleting unused snapshots. Or manually remove from __snapshots__ directory.
git diff threshold (10 lines) triggers updates for minor changes
Conservative threshold causes excessive updates. Increase: 'if [ "$LINES_CHANGED" -gt 50 ]; then' for major changes only. Or check specific files: test if modified file is component.
Loading reviews...
Join our community of Claude power users. No spam, unsubscribe anytime.
Automated accessibility testing and compliance checking for web applications following WCAG guidelines
Automatically generates or updates API documentation when endpoint files are modified
Automatically formats code files after Claude writes or edits them using Prettier, Black, or other formatters