Testing: load support modules of a dependency

Hi there

I’m currently working on a library that heavily depends on another library. In my tests, I would therefore like to use the test support modules in the test/support folder of the other library. However, the :elixirc_paths obviously only control the current MixPorject’s source folders. Is there a way to achieve this? Or is it generally bad design?

I can think your options are basically either:

  1. Copy the content of the library’s test/support into your own (manually or something like a git submodule),
  2. Ask the maintainer to make that support code public, as in Phoenix.ConnTest — Phoenix v1.6.6.

The challenge here is that the support code is an implementation detail at this point and you want to tie your code to that. This is fine, but somebody needs to pay the maintenance price (you - approach 1, or the library maintainer - approach 2).

2 Likes

EDITED most (all?) libraries on hex.pm won’t have the test directory packages so this advice is not valid unless you are using dependencies with :path or :git sources.

A third option might be to add the path of the dependency’s test/support directory to your elixir_rc paths like this in mix.exs

  def project do
    [
      .....
      elixirc_paths: elixirc_paths(Mix.env())
    ]
  end

  defp elixirc_paths(:test), do: ["lib", "test", "deps/the_dep/test/mix/support"]
  defp elixirc_paths(_), do: ["lib"]

Noting that this comes with the caveats mentioned by @stefanchrobot.

2 Likes

I think that the library author might decide not to package the test dir so it might be missing if you’re pulling the package from hex. See Phoenix for an example.

1 Like

Ahhhhh, yes, you are absolutely correct - I don’t package the test dir with my libs either so I should have thought that through better. Thanks for the correction.

1 Like

Thanks for your input. I managed to load the code like this in test_helper.exs: Code.require_file("test/support/helper.ex", "deps/other_lib").

However, (by coincident) I’m indeed loading the dependency through git and therefore get the test folder. But it’s not meant to stay that wy. So I guess I’ll have to copy over the helper. It’s not a big deal plus the maintenance price is payed by the user. :wink:

1 Like