japhib
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!
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
Hey there,
It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
Quite interesting article Google brought me. Didn’t find any mentions about it here.
What do you think in general? Would you use togethe...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
:warning: Security advisory: Decimal DoS vulnerability
A vulnerability has been published for decimal where very large exponents can cau...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sodapopcan
I’m not knocking your solution or anything but if you use Credo, its default is to force you to always provide the
:asyncoption be ittrueorfalse. As mentioned in your linked thread, the default isfalsefor a good reason and it’s good to be intentional about whether a test needs to be synchronous or not. Manually specifying it in all tests removes any question of whether or not its omission was accidental.EDIT: hmmm, I’m seeing this is a two month old post. It was clearly in my “suggest topics” but I read it as a new one. Sorry.
eksperimental
I run Credo and I have never been enforced to apply this rule.
sodapopcan
Ah, you are correct, it’s not enabled by default. It does ship with Credo but is in the
disabledsection by default.eksperimental
I didn’t know such a feature existed. Thanks for mentioning it. I think I will start using it.