Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
How do I test a recursive, asynchronous JavaScript function using fake timers?

I have a basic recursive function, which works correctly when executed. I would like to use Sinon's fake timers to test the function at each stage of execution.

However, it seems that the fake timers are only applying to the first call of the recursive function.

I'm curious if anyone can help figure out how to get the fake timers working all the way through.

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 })
}

Tested like so (with the help of this article):

it('retries after a given delay', function() {
  const clock = sinon.useFakeTimers()
  const promise = ensureCount(2, { delay: 200 })

  clock.tick(200)
  // Make some assertions.

  clock.tick(200)
  // Make some assertions.

  clock.tick(200)
  // Make some assertions.

  return promise
})

The expected console output (which is what happens without fake timers):

Attempt 0
Attempt 1
Attempt 2
✔ retries after a given delay (405ms)

The actual console output (which happens with 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 Question:

Is there a way to get the fake timers to apply to every step of the recursive call, rather than just the first one?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Turns out Sinon provides a .tickAsync() function for this that I hadn't seen. (Thanks to this comment).

This code resolves the issue:

it('retries after a given delay', async () => {
  const clock = sinon.useFakeTimers()
  ensureCount(2, { delay: 200 })

  await clock.tickAsync(200)
  // Make some assertions.

  await clock.tickAsync(200)
  // Make some assertions.

  await clock.tickAsync(200)
  // Make some assertions.

  clock.restore()
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda