japhib

japhib

How to default ExUnit.CaseTemplate to `async: true`

I’ve been using Elixir for a while and only today realized that the default async value for use ExUnit.Case is false. :man_facepalming:

It seems I’m not the only one who thought this was the case: Default value for ExUnit.Case async

Anyways, I spent some time looking at the code of ExUnit.Case as well as ExUnit.CaseTemplate, and was able to figure out a way to make async: true the default. So I thought I’d share it here:

defmodule MyApp.Case do
  @moduledoc false

  use ExUnit.CaseTemplate

  # Override `ExUnit.CaseTemplate.__using__` so we can manipulate opts and
  # default to `async: true`.
  #
  # For reference, see the original `defmacro __using__` that is injected
  # by ExUnit.CaseTemplate:
  # https://github.com/elixir-lang/elixir/blob/main/lib/ex_unit/lib/ex_unit/case_template.ex#L142
  defmacro __using__(opts) do
    parent = ExUnit.CaseTemplate.__proxy__(__MODULE__, default_async_true(opts))
    injected_frontmatter = frontmatter()
    {:__block__, [], [parent, injected_frontmatter]}
  end

  defp default_async_true(opts) do
    case Keyword.fetch(opts, :async) do
      {:ok, false} ->
        # If `async: false` is specified, leave opts alone.
        opts

      _ ->
        # If `async: false` is not specified, set `async: true`
        Keyword.put(opts, :async, true)
    end
  end

  defp frontmatter do
    quote do
      # Here's where you'd put useful aliases that normally go in `using do`
    end
  end
end

Now if you do use MyApp.Case, it’ll get async: true by default!

Most Liked

sodapopcan

sodapopcan

Ah, you are correct, it’s not enabled by default. It does ship with Credo but is in the disabled section by default.

eksperimental

eksperimental

I didn’t know such a feature existed. Thanks for mentioning it. I think I will start using it.

Where Next?

Popular in Discussions Top

WildYorkies
It seems that the more I read, the more I find Elixir users speaking about all the ways that Elixir is not good for x, y, and z use cases...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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 52673 488
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement