Hi,
I am happy to release the Repatch library for mocking and patching implementation in tests and anywhere else. It brings new possibilities which make it possible to make literally any test async.
You can install it and start trying it out in iex right now, while reading the post!
For example,
iex(1)> Mix.install [repatch: "~> 1.1"]
Resolving Hex dependencies...
Resolution completed in 0.01s
New:
repatch 1.1.0
* Getting repatch (Hex package)
==> repatch
Compiling 3 files (.ex)
Generated repatch app
:ok
iex(2)> Repatch.setup
:ok
iex(3)> Repatch.patch Range, :new, fn l, r -> Enum.to_list(Repatch.super(Range, :new, [l, r])) end
:ok
iex(4)> Range.new 1, 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Continue in the docs: Repatch — Repatch v1.1.0
Features
-
Patch any function or macro (except NIF and BIF). Elixir or Erlang, private or public, it can be patched!
-
Designed to work with
async: true
. Has 3 isolation levels for testing multi-processes scenarios. -
Requires no boilerplate or explicit DI. Though, you are completely free to write in this style with Repatch!
-
Every patch is consistent and applies to direct or indirect calls and any process you choose.
-
Powerful call history tracking.
-
super
andreal
helpers for calling original functions. -
Works with other testing frameworks and even in environments like
iex
or remote shell. -
Battle-tested in real world production project. Using Repatch instead of Patch reduced time of the whole test suite by 10-20% (depending on amount of cores on the test runner machine) by reducing the time of sync tests by 2 times.
Quick comparison with other tools
- Mox is a great library but it works only with behaviours which is a good approach from the architectural point of view, but fails to work in situations when you’re using 3rd-party library without behaviours, what makes you wrap every function you want to mock into the behaviour. However, Repatch inherits allowances and global mode approaches from Mox, but implements them in more efficient genserver-less way.
- Patch is a great library too and it has been a good example and Repatch borrows some code from it. Patch doesn’t work with
async: true
what has been a problem because test suite was getting slower every time it was using Patch. However, Repatch shares same ideas as Patch does. - Mimic is a library I’ve used and loved too and it shares most of it’s features with Repatch, however Repatch is more efficient in local-only (aka private in Mimic terms) patching and has a shared mode. Also, Repatch performs less amount of module recompilations (every module is recompiled at most once) in runtime.
New horizons
Features I describe here are unique to Repatch
and they bring new possibilities to write faster tests. Here is a short list of ideas:
- Patching
Application
and:application
modules for async-friendly application env. I did this in my production project and I was able to make every sync test in the project I have to become async. I did not finish the work and benchmarking this approach yet, but I expect around 30s speedup in the whole suite, which is a lot. - Patching
DateTime
module for time travelling in timestamps. Yes, it’s possible and works for some tests, but I haven’t figured a way to make it work for functions like:erlang.monotonic_time()
, so be careful when using these. - Patching
System
for async-friendly system env. I don’t usually write code which reads from system env in non-config files, but some people do and now you can test system env in async tests - Patching
Process.sleep
andProcess.send_after
for timeouts manipulation. This is also a risky thing to do, but, as the old Russian saying goes “Those who do not risk, do not drink champagne”!
Afterwords
Don’t be afraid to experiment with Repatch
and don’t be afraid to experiment in general. This library was born from understanding that long-lived tools and approaches are limited and there is always a way to move forward.