ream88

ream88

Wait_for_async_assigns - prevents "DBConnection.ConnectionError: client exited" errors in Phoenix LiveView tests

I’ve been running into DBConnection.ConnectionError: client exited errors in LiveView tests whenever using assign_async/3. The async tasks keep running after the test completes and crash when trying to access the closed database connection.

There’s been an open issue about this for a while, so I extracted a helper function we’ve been using:

test "loads data asynchronously", %{conn: conn} do
  {:ok, view, _html} = live(conn, ~p"/products")
  html = render_async(view)
  assert html =~ "Products"

  wait_for_async_assigns(view)
end

It waits for all async operations to complete before the test exits. The timeout defaults to ExUnit’s assert_receive_timeout but can be configured.

Installation

def deps do
  [
    {:wait_for_async_assigns, "~> 0.1.0", only: :test}
  ]
end

Import in your test case module:

defmodule MyAppWeb.ConnCase do
  use ExUnit.CaseTemplate

  using do
    quote do
      import Phoenix.ConnTest
      import Phoenix.LiveViewTest
      import WaitForAsyncAssigns

      @endpoint MyAppWeb.Endpoint
    end
  end
end

The package also includes a Credo check that warns when LiveView tests are missing wait_for_async_assigns/1:

# .credo.exs
%{
  configs: [
    %{
      name: "default",
      requires: ["deps/wait_for_async_assigns/lib/credo/check/wait_for_async_assigns.ex"],
      checks: %{
        enabled: [
          {Credo.Check.WaitForAsyncAssigns, []}
        ]
      }
    }
  ]
}

Links: Hex

GitHub:

Where Next?

Popular in Announcing Top

zachdaniel
Ash Framework 3.0: Official Release! I’m here with the fine folks at Gig City Elixir, pushing the button live on stage :sunglasses: T...
New
Flo0807
Hello everyone! I am excited to share our heart project Backpex with you. After building several Phoenix applications, we realized that...
New
simagyari
Hi Everyone, I’d like to share my first open-source package and my first Elixir project, GeoMeasure. It enables one to calculate propert...
New
zteln
Hello Elixir community, I’ve written a small embedded key-value database in Elixir called Goblin. It’s based on the Log-Structured Merge...
New
lucaong
CubDB is an embedded database written in pure Elixir, designed for robustness and minimal use of resources. It strives to be as developer...
New
dimamik
Vault is a lightweight Elixir library for immutable data storage within a process subtree. Due to Elixir’s actor model nature, it’s comm...
New
lostbean
Hello Elixir community! :wave: I’m excited to announce the first release of ReqCassette, a VCR-style record-and-replay library specifica...
New
LostKobrakai
I’ve recently created a small library phoenix_vite integrating the vite build tooling with phoenix. It provides an igniter.installer t...
New
billylanchantin
The Explorer library has been out for some time. But we just released the latest version (see below), and we thought we’d start posting u...
New
volcov
Hello, How is everyone? I hope you’re well =) I worked on a project with some legacy code that existed before Oban’s arrival. This leg...
New

Other popular topics Top

itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement