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. ![]()
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
Ah, you are correct, it’s not enabled by default. It does ship with Credo but is in the disabled section by default.
1
eksperimental
I didn’t know such a feature existed. Thanks for mentioning it. I think I will start using it.
1
Popular in Discussions
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
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
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
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
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
Hey everyone,
this has been brewing in my head some time and it came up again while reading Adopting Elixir.
GenServers, supervisors et...
New
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
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
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
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
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...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









