Skip to content

Commit 7b45f96

Browse files
rbinoRiccardo Binetti
andauthored
feat: prevent global mode in async tests (#110)
Similar to what Mox does, prevent this footgun by raising an exception Co-authored-by: Riccardo Binetti <riccardo.binetti@remote.com>
1 parent 9cd41c3 commit 7b45f96

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/mimic.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,14 @@ defmodule Mimic do
459459
```
460460
"""
461461
@spec set_mimic_global(map()) :: :ok
462-
def set_mimic_global(_context \\ %{}), do: Server.set_global_mode(self())
462+
def set_mimic_global(_context \\ %{})
463+
464+
def set_mimic_global(%{async: true}) do
465+
raise "Mimic cannot be set to global mode when the ExUnit case is async. " <>
466+
"If you want to use Mimic in global mode, remove \"async: true\" when using ExUnit.Case"
467+
end
468+
469+
def set_mimic_global(_context), do: Server.set_global_mode(self())
463470

464471
@doc """
465472
Chooses the mode based on ExUnit context. If `async` is `true` then

test/mimic_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,4 +1280,11 @@ defmodule Mimic.Test do
12801280
end
12811281
end
12821282
end
1283+
1284+
describe "set_mimic_global/1" do
1285+
test "raises if the test case is async" do
1286+
message = ~r/Mimic cannot be set to global mode when the ExUnit case is async/
1287+
assert_raise RuntimeError, message, fn -> set_mimic_global(%{async: true}) end
1288+
end
1289+
end
12831290
end

0 commit comments

Comments
 (0)