yordisprieto

yordisprieto

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.

Showing Posts 1 to 8

Marcus

Marcus

I am not sure if I understand you right. But you can setup compile path per environment.

defmodule MyApp.Mixfile do
  use Mix.Project

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

  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]
...
csadewa

csadewa

Ah, is this a discussion on what step elixir ecosystem/community should do to help reduce Testing Package code into production / ensure these Testing Support Package code not compiled in production? in that case i have no real comment

Original post:

Is including Test Support package/module directly on the library without any custom processing a viable option?
`oban` library include testing module (https://hexdocs.pm/oban/Oban.Testing.html#content) directly without any flag and rely on developer to not misuse it for production code
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?

yordisprieto

yordisprieto OP

I went for a little bit of an ugly route: https://github.com/straw-hat-team/beam-monorepo/blob/master/apps/one_piece_commanded/docs/explanations/enhancing-testing-environments.md

I rather do what Oban.Testing is doing, except that it is being compiled for production.

That is why I wish Elixir come up with some shared solution since we all doing whatever we think is correct trying to have a solution to the same problem :person_shrugging: not sure what is the right answer.

In that case, you would compile the test directory or whatever using elixirc_paths properly. In case of dependency packages, then I wouldn’t consider such code as “testing only”.

Sorry for the confusion @Marcus I can’t simply do that because the testing code lives in dependency packages, not my package.

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.

yordisprieto

yordisprieto OP

Fair enough,

Just for the sake of exercise to anticipate problems, what would be a good RFC to tackle the problem from Elixir itself?

yordisprieto

yordisprieto OP

I keep coming back to this after 2 years, but I am still not sure what is most beneficial for the ecosystem. I wish we would align so we do not need to learn new patterns or intended usage across different packages.

  • *.Test*
  • *.TestSupport*
  • .Testing*
  • *Test
  • [Insert N way Here]

There are so many different ways to do this. Aside from this, under what configuration key should we put the “feature flagging” (similar to Rust)?

# or alternative just making a point
config :my_scope, features: [test_support: :disabled]

I am not sure; it sounds like a good idea to me, but plenty of libraries would benefit from shipping with even more testing code included: Assertions, Mockings, Factories, Helpers … so many things

yordisprieto

yordisprieto OP

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

— All posts loaded —

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
515 12931 101
New
wmnnd
Hey folks, I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Ganna
Hi everyone, I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
gtcode
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews