Search code examples
elixirelixir-mixelixir-iex

iex.iexs file is not loading on test environment


I'am trying to load the iex.iexs file when debugging a test. I created an alias in Mix.exs that tries to import it.

defp aliases do
    [
      ...,
      "ex.file": [&import_iex_file/1, "compile"]
    ]
  end

  defp import_iex_file(_var) do
    require IEx.Helpers
    # This raises a File.Error if ~/.iex.exs doesn't exist.
    if "./.iex.exs" |> Path.expand() |> File.exists?() do
      IEx.Helpers.import_file("./.iex.exs")
    end
  end

When I run a test with iex -S mix test_path the file is loaded, but when it stop at the IEx.pry I can't access the aliases that I added at the .iex.iex file.


Solution

  • Note the file name should be .iex.exs. As you have noticed, running tests does not start an IEx session, so the file is not loaded. Instead, it might be better to do whatever setups you need for tests inside your tests/test_helper.exs file. If you really need to re-use the settings/shortcuts in your .iex.exs file, you can include it from your test_helper.exs file something like this:

    # tests/test_helper.exs
    require IEx.Helpers
    IEx.Helpers.import_file(".iex.exs")
    ExUnit.start()