Live Elixir Wizards Betweenisode recording today at 11AM EDT AyeSQL: Writing Raw SQL in Elixir

ExUnited - Easily spawn Elixir supervising nodes within ExUnit tests

ExUnited  is a Hex package designed to easily facilitate spawning  supervising  local Elixir nodes within tests. Unfortunately, I was not able to properly setup a spawned node for supervisioning with the Erlang  :slave.start_link/1  function. So I have written  ExUnited  to accomplish that, along with supporting  Mix.Config  configurations, additional loaded code, and a developer friendly way of writing assertions and refutations in the context of a spawned node which really improved the readability of the tests and more.

Features

  • Spawn nodes for testing purposes
  • Spin up “partially connected” vs “fully connected” nodes
  • Run in “verbose” mode which prints a colorized STDOUT of the nodes
  • Specify extra “code paths” which will be included ( config.exs  included)
  • Support child supervisioning within a spawned node
  • Exclude certain dependencies for spawned nodes
  • Easily assert and refute within the context of spawned nodes

Example

defmodule MyNodeClusterTest do
  use ExUnited.Case

  setup do
    {:ok, spawned} =
      ExUnited.spawn(
        david: [code_paths: ["test/nodes/beckham"], supervise: [David]]
      )

    on_exit(fn ->
      ExUnited.teardown()
    end)

    spawned
  end

  test "executes code in spawned node", spawned do
    assert :"captain@127.0.0.1" = Node.self()
    refute :"david@127.0.0.1" == Node.self()
    sentence = "The only time you run out of chances is when you stop taking them."

    as_node(:david, phrase: sentence) do
      assert :"david@127.0.0.1" = Node.self()
      refute :"captain@127.0.0.1" == Node.self()
      assert ^phrase = David.talk()

      foo = :bar
      refute match?(%{node: ^foo}, %{node: :foo})
    end
  end
end

See full example.

Enjoy the package! I would love to receive a shoutout and/or your feedback ;)