JasterV

JasterV

How to prevent mix test to start Application

Hello there!

First of all, I am a complete noob when it comes to Elixir, so the answer to this might be super basic but I need some help here!

I have an application with a supervision tree. In this tree there is a websocket client that will try to connect to a server when created. All good here, but the problem comes when I run mix test.

Running mix test apparently runs the supervision tree of my app and, obviously, my websocket client fails to connect to a server and triggers many errors.

Surprisingly my tests pass and mix test considers it a successful run, but I don’t because I don’t want all those error logs popping up on my CI!

The thing is, I don’t need this websocket client running tu run my tests. Any ideas what can I do here? And most important, why does the supervision tree starts on mix test?

Thank you!

Marked As Solved

cevado

cevado

There are a few options there:

  • write the children list of your app supervision tree to conditionally not start children that shouldn’t start in test environment.
  • configure your ws client to not log in test environment.
  • wrap your ws client around an adapter and mock it(using mox, probably) in tests.

I usually do the first one. using the configs for it. so my test.exs file has something like:

config :myapp, MyApp.SupChild,
enable: false,
...

and in the application.ex I have something like:

[
  Myapp.Repo,
  MyApp.Endpoint,
...
]
++
load(MyApp.SupChild)

where load/1 checks the application config for the enable key.

Also Liked

LostKobrakai

LostKobrakai

You can see that in action with phoenix, which starts the TCP parts of an endpoint only if configured to do so using config :my_app, MyAppWeb.Endpoint, server: true. mix phx.server is just a mix task enabling that config before starting the application. If the config is missing the “server” won’t be started.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
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
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement