Making Tests Repeatable

Let’s learn how to test functions that are not pure.

Testing impure functions

All of our tests are inside our functional core. In the core, calling a function with the same arguments will almost always result in the same output. The word “almost” is dangerous, though, because our whole strategy involves comparing our expectations to actual values. When we can’t have expectations from run to run, we must change our approach.

Timestamps and random numbers

Sometimes, functions are not perfectly pure. Functions that create timestamps or random numbers are famously challenging to test. We have both types of functions in our codebase. For example, recall the response test:

Get hands-on with 1400+ tech skills courses.