yordisprieto

yordisprieto

Testing Only Code and Dead-Code Elimination

Something I really appreciate from our ecosystem is that we trying to include “Test Support” files as part of the dependency packages.

Recently I have had a dilemma,

  1. Create two packages (a normal package, and a testing package) and publish them independently, for example: one_piece_commanded and one_piece_commanded_testing.
  2. Check for Mix.env() at compile-time and more the code
if Mix.env() == "test" do
  defmodule MyPackage.TestSupport do
    def hello do
      :hello_world
    end
  end
end

The first idea, is way too much work maintaining the packages. Incrementally becoming worst over time the more packages I have to maintain and whatnot.

The second idea, I feel dirty even suggesting it but I am not sure of any other solution that wouldn’t compile into production code that was only meant to be used in testing. Increasing the binary side (ideally, shouldn’t be the case).

Ideally, Elixir would help in the subject matter somehow, not sure what it would look like.

Any thoughts on the topic? How do you deal with the situation?

P.S: Kind of related to the trick of doing if Code.ensure_loaded?(Absinthe) do: .... to load extra code or not based on deps being installed.

Most Liked

LostKobrakai

LostKobrakai

oban is not the only library doing that. The same applies to Plug.Test, Phoenix.ConnTest or Phoenix.LiveViewTest.

If test helpers are not meant to be bundled in prod code I’d consider a compile time config like config :my_app, compile_test_helpers: false to disable them. Every other toggle will be a heuristic and therefore just waiting to not work for some user.

Generally I’d really consider hard, why including test helpers in prod code would be problematic though. What if the usecase is a test runner and prod actually runs tests?

LostKobrakai

LostKobrakai

Imo the issue here is the assumption that test helpers are code only used in tests. For a library I don’t think that’s an assumption, that should be made and therefore I actually support the existing approaches of having those modules be handled just like any other.

There are some valid concerns like file size bloat, but those are optimizations and also likely most relevant in really constraint environments, where you’d have the same concern not only about modules holding test helpers, but rather any module. So for the file size concern it makes sense to use a wholistic approach of dealing with it, rather than thinking about those handful of modules specific to test helpers.

Last Post!

yordisprieto

yordisprieto

To be clear, that wouldn’t work after; Mix.env() inside a dependency for a given app since to always be :prod
That is out of the equation and must be done differently

Where Next?

Popular in Discussions Top

rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14350 124
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement