Is the following a pure function?
function test(min,max) {
return Math.random() * (max - min) + min;
}
My understanding is that a pure func
No, it isn't. You can't figure out the result at all, so this piece of code can't be tested. To make that code testable, you need to extract the component that generates the random number:
function test(min, max, generator) {
return generator() * (max - min) + min;
}
Now, you can mock the generator and test your code properly:
const result = test(1, 2, () => 3);
result == 4 //always true
And in your "production" code:
const result = test(1, 2, Math.random);