Run stale tests on file change in Elixir

Mix is an awesome tool but most Elixir beginners not aware of all its features. mix test —-stale is the one them and can make your workflow much smoother. It tells mix to run only tests which reference modules that changed from the latest test run.

Literally, this means that if you have changed some code in your application, the mix will run only the tests that touch this code or it’s dependents. This saves a lot of time if you trying to follow TDD in a mid-to-large sized project.

But to run this command we still have to switch between a code editor and a console. Fortunately, mix test supports --listen-on-stdin flag that configures mix to read stdin and restart the tests after receiving a newline. Now we only need a way to watch for the file system events.

Here is simple bash script for that (tested on macOS):

It utilizes fswatch to read the file system events, it filters out the files ignored by git along with some common directories that we are not interested in. Then it writes a new line to stdout. The result should look like this one:

Look ma no hands!