Freedom_Ben
Freedom_Ben

Reputation: 11923

Is there a way to rerun the initial test/mix command again from within the iex prompt?

If I run a test with iex, like:

iex -S mix test test/path/file_test.exs

and it completes, it drops me into a regular iex prompt where it waits for a break point. Great! However I can't figure out how to re-run the test so I have to <C-c> <C-c> <up> <enter> to run it again. It's a bit of a pain and seems unnecessary.

I want to run it inside iex so that my pry breakpoints will work. Is there a way to rerun the initial test/mix command again from within the iex prompt?

Upvotes: 3

Views: 267

Answers (1)

Deepak Sharma
Deepak Sharma

Reputation: 166

There is a way to do that but that is very unattractive. Mix keeps track of all the tests that have been run indefinitely (until VM dies).So starting iex -S mix and executing Mix.Task.run "test' will work the first time around but will assume the "test" task already ran on the second invocation

ExUnit also has state that is assumed to be initialized once (with all the test suites to run) and gets emptied out as you run the tests. Unfortunately, that initialization happens at compile time of the test see ExUnit.Case source

 @doc false
  defmacro __before_compile__(_) do
    quote do
      if @ex_unit_async do
        ExUnit.Server.add_async_case(__MODULE__)
      else
        ExUnit.Server.add_sync_case(__MODULE__)
      end

      def __ex_unit__(:case) do
        %ExUnit.TestCase{name: __MODULE__, tests: @ex_unit_tests}
      end
    end
  end

Solution:

$MIX_ENV=test iex -S mix phx.server
iex(1)>Mix.Tasks.Test.run(["test file path"])
iex(2)>Kernel.ParallelCompiler.files(["test file path"]) 
iex(3)>Mix.Tasks.Test.run(["test file path"])

Upvotes: 2

Related Questions