Existing test failed to detect function calls that happened after the specified delay
Existing test relied on setTimeout and done
New test ensures that each function is called is called once, in order, after the specified delay
New test refactor leverages timer mocks to make test code more declarative
Examples:
Solution that should pass all tests:
const solution = (arr, time, i = 0) => {
if (i >= arr.length) return;
setTimeout(() => {
arr[i]();
solution(arr, time, i + 1);
}, time);
};
Solution that should fail, but passes existing tests:
const solution = (arr, time, i = 0) => {
if (i < arr.length) {
// error introduced with time + time: the delay doubles between each call
solution(arr, time + time, i + 1);
setTimeout(arr[i], time);
}
}
Overview:
setTimeout
anddone
Examples:
Solution that should pass all tests:
Solution that should fail, but passes existing tests: