Testing · Junior · Question 40 of 78
How do you make a test that uses Math.random() deterministic?
- ✓Correct answer: Mock
Math.random(e.g.vi.spyOn(Math, "random").mockReturnValue(0.5)) or inject the random source - 2Run the test several times and take the majority result
- 3Mark it with
it.skip
Explanation
Random values, current time and network are the usual sources of non-determinism. Fix them in tests.