How to get --warnings-as-errors in tests?

I’d like to get compiler warnings as errors in my tests, because in my experience, such warnings quite often uncover real bugs.

What I’ve tried before:

  • alias configuration in mix.exs, which exactly I forgot
  • test_elixirc_options: [warnings_as_errors: true, docs: false, debug_info: false] in project/0 in mix.exs, which looks like the correct thing to do, but did not have the desired effect.

By desired effect I mean that not only I want such warnings to be shown, but to cause an exit with a non-zero exit status.

This looks highly relevant: elixir/lib/mix/lib/mix/tasks/test.ex at v1.17.0 · elixir-lang/elixir · GitHub

The documentation some lines above suggests

https://github.com/elixir-lang/elixir/blob/v1.17.0/lib/mix/lib/mix/tasks/test.ex#L210C11-L210C87

which does not work for me.

Your link has a comment right before it that mentions that the --warnings-as-errors flag is not passed down to tests. If you want to do this you are going to need two different steps.

$ MIX_ENV=test mix compile --warnings-as-errors
$ mix test

Personally, I dislike using things like --warnings-as-errors in dev and test. There are times when I want to compile or test my code when it is in an unfinished state. Though, I always run --warnings-as-errors within CI when I try to get my changes merged into the base branch.

Another way to set warnings as errors in tests, you can configure this in your test_helper.exs by adding:

# test_helper.exs
Code.put_compiler_option(:warnings_as_errors, true)
ExUnit.start()

check the official documentation: Code.put_compiler_option/2.

2 Likes