I've been working with a basic recursive function that performs as expected when run. To thoroughly test its operation at each stage, I want to utilize Sinon's fake timers.
Unfortunately, it seems that the fake timers are only affecting the initial call of the recursive function.
I'm hoping someone might have a solution for ensuring that the fake timers are active throughout the entire process.
For example:
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function ensureCount(count, { attempt, delay }) {
attempt = attempt || 0
console.log('Attempt', attempt)
if (attempt === count) {
return
}
await wait(delay)
await ensureCount(count, { attempt: attempt + 1, delay })
}
Testing method used (with guidance from this resource):
it('retries after a given delay', function() {
const clock = sinon.useFakeTimers()
const promise = ensureCount(2, { delay: 200 })
clock.tick(200)
// Add your assertions here.
clock.tick(200)
// Add your assertions here.
clock.tick(200)
// Add your assertions here.
return promise
})
Expected console output (without fake timers):
Attempt 0
Attempt 1
Attempt 2
✔ retries after a given delay (405ms)
Actual console output (using fake timers):
Attempt 0
Attempt 1
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
The Issue:
Is there a method to make the fake timers apply to every iteration of the recursive call rather than just the first one?