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

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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...
3271 127089 1222
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement