← Knowledge

Public EcoRouter Knowledge · AI-generated content ·

How can I use Claude Code hooks to automatically run linting and tests on every change?

The question

How can I use Claude Code hooks to automatically run linting and tests on every change?

Efficient

To use Claude Code hooks for automating tasks like linting and testing, you can leverage the .claude/hooks configuration. This allows you to define custom actions that trigger during the lifecycle of your interaction with the tool.

Here is how to set it up:

1. Locate or create the hooks directory

In the root of your project, ensure there is a folder named .claude/hooks.

2. Add your hook script

Claude Code executes scripts placed in this directory based on the specific event. To run tasks on every change (typically tied to the pre-commit or post-edit phase, depending on your workflow), you can add a shell script.

For example, create a file named .claude/hooks/post-edit.sh:

bash
#!/bin/bash
# Run your linting and testing commands
echo "Running project checks..."

# Replace these with your project's specific commands
npm run lint && npm test

# Exit with status 1 if you want to alert the user of failures
if [ $? -ne 0 ]; then
  echo "Checks failed!"
  exit 1
fi

3. Make the script executable

You must ensure the system has permission to run the script:

bash
chmod +x .claude/hooks/post-edit.sh

Important Considerations:

  • Performance: Because these hooks run after edits, keep your linting and test suites efficient. If your test suite is very large, consider creating a specific "smoke test" script that runs a subset of critical tests to provide faster feedback.
  • Project Specifics: The commands inside the hook depend entirely on your environment. If you are working in a Python project, you might use ruff or pytest; in a Node.js project, eslint or jest.
  • Documentation: If you are unsure if your specific version of Claude Code supports a particular hook name, you can check the official documentation or verify the output in your terminal when you trigger an edit.

If you encounter issues where the hook is not triggering as expected, please let me know which version of the CLI you are using, and I can check if there are specific requirements for your configuration.

Answering this used an estimated 90% less than EcoRouter’s configured frontier baseline

Routed to
Efficient
Tokens
377 in / 484 out
Cost
$0.0008
Baseline
$0.0084

Figures recorded by EcoRouter when this answer was generated, and fixed at that moment. Cost comparisons are against a configured reference model, not a measurement of electricity, carbon or water.

Ask about this

Ask EcoRouter a follow-up using this Knowledge as context. Nothing becomes public unless you choose to publish it.

Ask a follow-up
3 views

Comments

No comments yet.