Using Claude Code hooks to run tests automatically is useful for one common problem: you may have written “run tests after changes” in CLAUDE.md, but Claude Code may forget, run too much, or summarize a failure too loosely.
Hooks turn “please remember” into “run this when the event happens.” For testing, the two most useful events are PostToolUse and Stop.
Quick Answer
Use PostToolUse when you want a lightweight check right after Claude edits or writes a file. Limit the matcher to tools such as Edit|Write.
Use Stop when you want a final reminder or narrow verification before Claude finishes a turn. Do not put the full test suite there by default.
A stable setup usually looks like this:
PostToolUseruns lightweight checks, such as formatting, type checking, or file-type-based focused tests.Stopprints a final verification checklist or runs a narrow final script.- Test failures return a short summary, not a full log dump.
- Project hooks live in
.claude/settings.json, with real logic in.claude/hooks/. - Test the setup in a small repository before using it across a team.
Decide What Should Run
Do not start with:
|
|
The closer a command is to each edit, the shorter and more deterministic it should be.
| Scenario | Suggested check |
|---|---|
| A frontend component changed | Related unit test, typecheck, lint |
| A backend function changed | Related package unit test |
| A config file changed | Config validation or build dry run |
| Markdown changed | Link or front matter check |
| Turn is ending | Minimal final verification reminder |
| Commit or release | Full tests and build, usually manual or CI |
Hooks should not replace CI. They are a local safety net that helps Claude notice obvious problems quickly.
Recommended Layout
Do not pack complex logic into a one-line settings.json command.
|
|
On Windows, use PowerShell scripts if that is what the team normally runs:
|
|
Use PostToolUse After Edits
PostToolUse fires after a tool call. For automated tests, usually match file-writing tools only.
.claude/settings.json:
|
|
This registers the hook. The file-to-test logic belongs in the script.
Bash Example
.claude/hooks/run-related-tests.sh:
|
|
Then make it executable:
|
|
PowerShell Example
.claude/hooks/run-related-tests.ps1:
|
|
Corresponding settings:
|
|
Test the script manually before wiring it into hooks.
Keep Failure Output Short
Hook output enters Claude Code’s workflow. If a test fails, Claude may use that output to continue fixing the issue. Avoid dumping huge logs.
For example:
|
|
Hooks should run deterministic commands. Avoid interactive prompts, random ports, flaky services, or commands that require manual confirmation.
Use Stop for Final Checks
PostToolUse is for immediate checks. Stop is better for “before this turn ends, confirm what remains.”
|
|
.claude/hooks/final-check.sh:
|
|
Start with reminders before automatically running expensive commands.
When Not to Auto-Run Tests
Be careful when:
- unit tests take more than about 30 seconds;
- tests need databases, browsers, queues, or external APIs;
- tests mutate local data;
- logs are huge;
- there is no stable focused test command;
- Claude often edits many unrelated files at once.
In those cases, make the hook suggest a command instead of running it.
Risk-Based Strategy
| Risk | Automated action |
|---|---|
| Low | formatting, single-file lint, Markdown checks |
| Medium | typecheck, related unit tests, config validation |
| High | full tests, E2E, build, migration checks |
Low-risk checks can run in PostToolUse. Medium checks can run by file type. High-risk checks should usually stay in CI or require human confirmation.
CLAUDE.md and Hooks
CLAUDE.md is good for principles:
|
|
Hooks are good for execution:
|
|
Use both: CLAUDE.md says how to think; hooks make the action happen.
Troubleshooting
If a hook does not run:
- Run
/hooksin Claude Code and check registration. - Validate
settings.json. - Check event names such as
PostToolUseandStop. - Check the matcher, such as
Edit|Write. - Check script paths.
- On macOS/Linux, confirm execute permissions.
- On Windows, check PowerShell execution policy and quoting.
- Start with
echo hook fired. - Do not start with complex test commands.
- If output is too long, summarize it.
Team Rollout
For team repositories:
- Start with final-check reminders.
- Automate low-risk checks such as formatting, Markdown, and typecheck.
- Add related unit tests by directory or file type.
- Leave full tests to CI or manual confirmation.
- Provide both
shandps1scripts if the team uses mixed systems.
References
Summary
Auto-running tests with Claude Code hooks is not about putting npm test into a config file. It is about splitting verification into small, relevant, repeatable checks.
Use PostToolUse for lightweight checks after edits, Stop for final reminders or narrow verification, and keep full test suites in CI or manual pre-release checks. Put principles in CLAUDE.md, actions in .claude/hooks/, and verify registration with /hooks.