japhib

japhib

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!

Showing Posts 1 to 4

sodapopcan

sodapopcan

I’m not knocking your solution or anything but if you use Credo, its default is to force you to always provide the :async option be it true or false. As mentioned in your linked thread, the default is false for 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

eksperimental

I run Credo and I have never been enforced to apply this rule.

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.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
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...
2977 94592 917
New
cblavier
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
heathen
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
AstonJ
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
Null-logic-0
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
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New

Other Trending Topics Top

JesseHerrick
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
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews