The improvements in 1.8 are very helpful, but it doesn’t do the shrinking that I do in assertions, and it also doesn’t expand all variables in the argument to assert. For example, a common way to test unordered list equality is something like this:
list = [1, 2, 3]
assert Enum.all?([2, 1, 4], & &1 in list)
That gives us this failure:
1) test example (AssertionsTest)
test/assertions_test.exs:8
Expected truthy, got false
code: assert Enum.all?([2, 1, 4], &(&1 in list))
arguments:
# 1
[2, 1, 4]
# 2
#Function<6.120201396/1 in AssertionsTest."test example"/1>
stacktrace:
test/assertions_test.exs:10: (test)
So we can see the one list, but we can’t see the elements in the other list that we’re checking for unordered equality. And then there’s also the problem of that not actually checking equality, which many people think it does ![]()
But if we use assert_lists_equal then we write the test like this:
list = [1, 2, 3]
assert_lists_equal(list, [2, 1, 4])
Which gives us this output:
1) test example (AssertionsTest)
test/assertions_test.exs:8
Comparison of each element failed!
code: assert_lists_equal(list, [2, 1, 4])
arguments:
# 1
[1, 2, 3]
# 2
[2, 1, 4]
left: [3]
right: [4]
stacktrace:
test/assertions_test.exs:10: (test)
So there we can see the differing elements between the two lists in left: and right: , which I find really helpful. Since the standard diffing that we do with something like list1 == list2 assumes that order matters, it doesn’t work when comparing lists where the order doesn’t matter (which is a great deal of the time). I thought the best way to show the diff would be to remove the common elements.
I also do this shrinking when comparing structs/maps with assert_maps_equal/3 - it will just show you what’s different instead of showing you the whole big map, and you still get the colored diff if the maps are nested.






















