Mix test --file-watch?

I love this Haskell Stack feature:

  --file-watch             Watch for changes in local files and automatically
                           rebuild. Ignores files in VCS boring/ignore file

I do stack test --file-watch and it continuously re-tests as I work. Has anyone configured anything similar for mix test? Maybe using Guard?

1 Like

You can probably build something over mix test --listen-on-stdin.

–listen-on-stdin - runs tests, and then listens on stdin. Receiving a newline will result in the tests being run again. Very useful when combined with --stale and external commands which produce output on stdout upon file system modification.


1 Like

There is https://github.com/lpil/mix-test.watch

Also https://medium.com/@andrew_dryga/run-stale-tests-on-file-change-in-elixir-6f8aac829973


Here’s two language-agnostic options I have used in the past:


git ls-files **/*.ex* | entr -cdp mix test [--stale]


watchexec -c -p -e 'ex,exs,lock' -- mix test [--stale]

Here’s an Elixir-specific snippet from my dotfiles that I’m not sure of the provenance of, and that seems kind of overwrought, but I think it might have been derived from a José/Plataformatec tweet or something?

fswatch --latency=0.01 --one-per-batch lib/ test/ | MIX_ENV=test mix do test --stale, run --no-halt -e "IO.gets(:stdio, ''); IO.puts 'Restarting...'; :init.restart()"

And a variant for umbrella apps:

fswatch --latency=0.01 --one-per-batch apps/ config/ mix.exs mix.lock | MIX_ENV=test mix do test --stale, run --no-halt -e "IO.gets(:stdio, ''); IO.puts 'Restarting...'; :init.restart()"

Edit: I wasn’t entirely mistaken, this was a José gist that maybe got tweeted about: https://gist.github.com/josevalim/a5e747c90a72e40a4b52062ea6919877


Thanks! mix-test.watch works perfectly, including --stale support and clearing the screen before each test run.


I use Cortex myself:


Sweet. I’m actually back to the drawing board, because mix-test isn’t working for me in a Docker container with a Bind mount. I may need a tool that’s configureable enough to do polling.

This is really slick, thanks for sharing!

1 Like