protoboolean

protoboolean

Test helpers defined in their own files under the `test` folder not loaded; (and why ElixirLS ignores test_helper.exs contents?)

I’m going through the “Elixir For Programmers” course by Dave Thomas, which uses a simple game as didactic example.

I have written some unit tests for the game, extracted a TestPlayer module with some helper functions, and figured out I could have it loaded by placing it in test_helper.exs (placing it in its own file test_player.ex was like not having written it). Later I discovered that ElixirLS (with VSCode) seemed to ignore the contents of test_helper.exs: jump to definition doesn’t work.

I decided to try to extract the module again in its own file to se if it was going to make ElixirLS happier: test/test_helper/test_player.exs first but file compilation failed, then changed to .ex and still didn’t work.

So I wonder if I can define helper modules in their own files somewhere under the test folder and, ideally, have them loaded both when testing and by ElixirLS?

I can imagine projects where test-helpers would make test_helper.exs very large if that’s the only place where they can be defined, :thinking:

Skimming through three books: Elixir in Action, Programming Elixir 1.6, Testing Elixir; didn’t answer my question.
On Testing Elixir at p.41 I found a test-double defined in integration_tests/soggy_waffle/fake_weather_api.ex; but that integration_tests folder seems non-Mix compliant, it leaves me wondering where they’d actually put it in a project, anyway my .ex file placed under the test folder were ignored.

Marked As Solved

Aetherus

Aetherus

Check your mix.exs, the project/0 function should look like this:

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

and in the same file, there should be a private function elixirc_paths/1:

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

Also Liked

03juan

03juan

Check the Elixir LS: Mix Env setting in vscode is set to test. (the default)

I had mine set to dev and when importing the module from a helper file in ./test/support/fixtures/accounts_fixtures.ex the jump to definition was not going to the imported user_fixture() function, but it worked after changing the setting, deleting ./.elixir_ls/ and letting the extension rebuild.

Edit: Sorry, this is my own “XY problem”, probably not relevant to your question if using default settings and @Aetherus’s answer is correct when it comes to non-standard paths.

protoboolean

protoboolean

@Aetherus answer did the trick. Thanks anyway for your suggestion, I’ve learned something from it. :slight_smile:

03juan

03juan

Happens to the best of us :sweat_smile:

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement