Run Tests Watcher
All-in-one hook covering automatically, quick, tests, after. Includes structured workflows, validation checks, and reusable patterns for post tool.
Run Tests Watcher
Automatically runs quick tests after code modifications to catch breaking changes immediately during development sessions.
When to Use This Hook
Attach this hook when you need to:
- Get instant feedback on whether code changes break existing tests
- Enforce a continuous testing discipline during Claude Code editing sessions
- Catch regressions within seconds of making changes rather than at commit time
Consider alternatives when:
- Your test suite takes more than 10 seconds to run and would slow down the editing flow
- You prefer to run tests manually after completing a logical set of changes
Quick Start
Configuration
name: run-tests-watcher type: hook trigger: PostToolUse category: post-tool
Example Trigger
# Hook triggers after Claude edits a source file claude> Edit src/utils/calculateTotal.ts # Quick tests run automatically after the edit
Example Output
Running quick tests...
PASS src/__tests__/calculateTotal.test.ts (0.8s)
Tests: 4 passed, 4 total
Time: 1.2s
Tests passed
Core Concepts
Test Watch Overview
| Aspect | Details |
|---|---|
| Trigger | Runs after every Edit tool operation |
| Test Command | npm run test:quick (configurable) |
| Prerequisite | Checks for package.json before running |
| Feedback | Emits pass/fail status message |
| Failure Mode | Non-blocking warning on test failure |
Test Watch Workflow
Edit Completes
|
package.json exists?
/ \
No Yes
| |
Skip Run test:quick
|
┌────┴────┐
| |
Pass Fail
| |
"Tests "Tests may
passed" need attention"
Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
test_command | string | "npm run test:quick" | Command to execute for quick tests |
timeout_sec | number | 30 | Maximum time to wait for test completion |
silent_pass | boolean | false | Suppress output on successful test runs |
block_on_fail | boolean | false | Prevent further edits when tests fail |
related_only | boolean | false | Run only tests related to the changed file |
Best Practices
-
Create a Dedicated test:quick Script - Define a
test:quicknpm script that runs a fast subset of your test suite (unit tests only, no integration). Keep execution under 5 seconds for a responsive editing experience. -
Use Related Test Detection - Configure Jest's
--findRelatedTestsor a similar feature to run only tests that import the modified file. This keeps feedback fast even in large codebases. -
Set Appropriate Timeouts - A 30-second default timeout prevents runaway test processes from blocking your session. Adjust this based on your test suite's characteristics.
-
Non-Blocking by Default - Keep
block_on_faildisabled so test failures inform you without preventing further edits. Some changes intentionally break tests temporarily during multi-file refactors. -
Pair with Coverage Hooks - Combine the test watcher with a coverage threshold hook to ensure that new code includes tests. The watcher confirms existing tests pass; coverage hooks confirm new code is tested.
Common Issues
-
test:quick Script Missing - If
package.jsondoes not define atest:quickscript, the hook fails silently. Add atest:quickscript or update the hook to use your existing test command. -
Slow Tests Block Editing - If quick tests take too long, they create a bottleneck after every edit. Profile your test suite and move slow tests (API calls, database queries) to a separate suite.
-
False Failures from Shared State - Tests that share global state may fail intermittently after edits. Use
--runInBandfor deterministic execution or ensure proper test isolation with setup/teardown.
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Pre-Commit Security Scanner
Pre-commit hook that scans staged files for hardcoded secrets, API keys, passwords, and sensitive data patterns before allowing commits.
Agents Md Watcher
Streamline your workflow with this automatically, loads, agents, configuration. Includes structured workflows, validation checks, and reusable patterns for automation.
Automated Build Inspector
Boost productivity using this automatically, trigger, build, processes. Includes structured workflows, validation checks, and reusable patterns for automation.