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

127
Visualizações
How to test promise before it runs finally with JEST

I have a function like this:

let settings = {
  isRun: false
}
function testPromise(promise) {
  settings.isRun = true;
  return promise.finally(() => settings.isRun = false;)
}

How can test isRun = true when run testPromise before finally expression run. I tried with

it('should isRun true when testPromise running', async () => {
   expect(settings.isRun).toBeFalsy();
   await testPromise(Promise.resolve('run')).then(() => {
     expect(settings.isRun).toBeTruthy();
   }
   expect(settings.isRun).toBeFalsy();

but it does not work

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

0

I think you can execute but not wait for the promise to observe the intermittent value change you want to see.

it('should isRun true when testPromise running', () => {
    expect(settings.isRun).toBeFalsy();
    const promise = testPromise(Promise.resolve('run'));
    // At this point, promise is not resolved yet, so the callback in finally has not executed yet.
    expect(settings.isRun).toBeTruthy();
    // If you want to test it changed back to fasle
    promise.then(() => {
        expect(settings.isRun).toBeFalsy();
    });
}
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