Multiple (Custom) Test Environments?

I recently added Wallaby to a project.

To increase my test speed, I made a separate MIX_ENV hoping to run my browser tests in that environment while the unit tests run in the default test environment. (Separating the tests is achieved using ExUnit’s tags).

Running the browser tests in my custom environment fails with this error:

** (Mix) “mix test” is running in the “wallaby” environment. If you are running tests from within another command, you can either:

  1. set MIX_ENV explicitly:
    MIX_ENV=test mix test.another
  2. set the :preferred_cli_env for a command inside “def project” in your mix.exs:
    preferred_cli_env: ["test.another": :test]

I guess this means all tests must run in the same (test?) environment.

So, is it possible to separate my tests and get them to run in different environments?
How can I achieve it (or why can’t I if it’s impossible)?

Thanks for any help you can offer.

I wouldn’t change the MIX_ENV for tests, I would do add an alias mix test.features which is aliased to mix test --only feature.

This would also require added the preferred_cli_env for that new command.

I’m not sure what there is to gain from changing the MIX_ENV.

Do you simply which to have a command to run unit tests and a command to run wallaby tests?

Thank you @mhanberg

I used an alias, so I already have 2 separate commands for my test (mix test and mix browser.test).

Also, I didn’t set my MIX_ENV directly. I set my environment using preferred_cli_env

Initially, it was:

preferred_cli_env: ["browser.test": :test].

However, the problem with this was that even when I ran only the unit tests (mix test), the tests were still slow.

I observed that chromedriver still started even though none of the Wallaby tests were going to run.

So, I setup another environment (wallaby) exactly like the test environment so that I can achieve:

{:wallaby, “~> 0.30.0”, runtime: false, only: :wallaby}

and

preferred_cli_env: ["browser.test": :wallaby].

This was the setup that produced the error in the OP.

I hope the situation is clearer.

My goal is to have Wallaby removed from the test environment and then have a similar environment to run the wallaby tests in.

How can this be achieved?

You can probably (with runtime: false) add to your test_helper.exs

if System.get_env("FEATURE_TESTS") == "true" do
  Application.ensure_all_started(:wallaby)
end

And then run your browser tests with FEATURE_TESTS=true mix test --only feature or maybe make an alias "browser.test": [fn _ -> System.put_env("FEATURE_TESTS", "true") end, "test --only feature"] (I think the anonymous function literal should work there but but it’ll need to be extracted)

3 Likes