cc-hook-test

March 24, 2026 · View on GitHub

npm version

Test runner for Claude Code hooks. Auto-detects hook type and generates test cases.

npx cc-hook-test ~/.claude/hooks/destructive-guard.sh
  cc-hook-test — destructive-guard.sh
  Detected type: PreToolUse

  PASS empty input passes
  PASS safe command passes
  PASS blocks rm -rf /
  PASS blocks rm -rf ~
  PASS allows rm -rf node_modules
  PASS blocks git reset --hard

  6/6 tests passed

Usage

# Auto-test a hook (generates test cases based on script content)
npx cc-hook-test <hook-script>

# Test with custom test cases
npx cc-hook-test <hook-script> tests.json

# Generate a test template
npx cc-hook-test --generate destructive
npx cc-hook-test --generate branch
npx cc-hook-test --generate secret
npx cc-hook-test --generate custom

Test File Format

[
  {
    "name": "blocks rm -rf /",
    "input": { "tool_input": { "command": "rm -rf /" } },
    "expect_exit": 2,
    "expect_stderr": "BLOCKED"
  },
  {
    "name": "allows ls",
    "input": { "tool_input": { "command": "ls -la" } },
    "expect_exit": 0
  }
]

What It Auto-Detects

Hook ContentDetected TypeGenerated Tests
rm -rf, git reset --hardPreToolUse (destructive)rm variants, git reset, git clean
git push, main, masterPreToolUse (branch)push to main, force push, feature push
.env, secret, credentialPreToolUse (secret)git add .env, git add src/
py_compile, file_pathPostToolUse (syntax)non-existent file
stop_reasonStopnormal/user stop

Supports

  • Bash hooks (.sh)
  • Node.js hooks (.js)
  • Python hooks (.py)

License

MIT