Keeping your test suite in sync with code changes can save minutes every day. In this guide you’ll learn how to automatically rerun your project’s test suite on file changes using the Unix entr command in a cross‑platform shell script.
entr test suite auto rerun: How It Works
When a file matching a pattern is modified, entr executes a command you specify. This triggers an immediate test rerun, giving you fast feedback without manual intervention.
Setting Up entr in a Cross‑Platform Script
Install entr on your system. On macOS you can use brew install entr, on Linux apt-get install entr, and on Windows via WSL or Git Bash. Once installed, create a small shell script that pipes your file list to entr.
Use the following script:
#!/bin/sh
find . -type f -name '*.md' | entr -p sh -c 'npm test'
Here, find collects all .md files, and -p tells entr to preserve the command’s exit status.
Writing the Rerun Script
Compose a script that defines which files to watch and what command to execute. Below is a step‑by‑step guide:
- Identify the pattern of files you care about (e.g.,
**/*.spec.js). - Choose the test runner command (such as
npm testorpytest). - Combine them with
entrusing a pipe or xargs.
Cross‑Platform Considerations
Because entr behaves consistently across Unix‑like shells, you can use the same script on macOS, Linux, and Windows (via WSL or Git Bash). The only nuance is line‑ending handling; use dos2unix if needed.
- Use forward slashes in paths for portability.
- Avoid Windows‑specific commands unless wrapped in a compatibility layer.
- Test the script in each environment before committing.
Troubleshooting and Trade‑offs
While entr is lightweight, it does not debounce rapid file changes. If your workflow triggers many events, consider adding a short sleep or using fswatch instead.
- Run the script in a terminal to see live output.
- Check that the command exits with a non‑zero status on failure.
- If tests hang, ensure no infinite loops in the test runner.
Real‑World Example
A front‑end team uses entr to watch TypeScript source files and automatically run Jest tests. The setup reduced their feedback loop from 30 seconds to under 5 seconds, accelerating bug fixes.
Conclusion
Implementing an entr test suite auto rerun workflow is a simple yet powerful way to keep your tests close to your code. Try adding the script to your justfile or npm scripts and watch productivity soar.