Create an alias for another task in mix.exs?

Background

So I am using this library mix-test.watch from @lpil which I really like.

Usually I just run mix test.watch and I let the process running.

As a bonus to this library, you can also execute it by passing the --only flag. For example, if I want to watch only my “super_duper_tests” I can do it like this: mix text.watch --only super_duper_tests and this will only watch all the tests that have, for example, the @moduletag :super_duper_tests on them.

Enter Alias

So, this is all fun, but I am lazy and I don’t like typing. So I want to create an alias like this:

mix test.watch.super_duper that will be translated to mix text.watch --only super_duper_tests.

To achieve this I tried the following on my mix.exs file:

defp aliases do
  [
    "test.watch.super_duper": ["test.watch --only super_duper_tests"]
  ]
end

Problem

You may have guessed, since I am posting here, that this is not working. When I try my new alias, I get the error message Could not find test.watch task.

So obviously I am doing something wrong in the configuration.

Questions

How can I create an alias for mix test.watch?

How did you defined the dependency? Are you running in correct Mix environment?

1 Like

This is how I defined it:

[{:mix_test_watch, "~> 1.0", only: :dev}]

Perhaps it should be:

[{:mix_test_watch, "~> 1.0", only: [:dev, :test]}]

right?

EDIT

Yep, it needed to be [{:mix_test_watch, "~> 1.0", only: [:dev, :test]}].

1 Like

BTW I prefer to use entr or watchman with mix test --listen-on-stdin

4 Likes

You mean:

right?
I couldn’t find any github repo for entr, could you post a link?

I personally use watchexec:

watchexec -e 'ex,exs,eex,leex' -- 'mix format && mix test --stale'
4 Likes

http://eradman.com/entrproject/

2 Likes

Blog post showing usage of entr
https://jvns.ca/blog/2020/06/28/entr/

2 Likes