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

157
Visualizações
Chai eventually erroneously passing

Why does the test expressed with eventually pass? I'm obviously missing something in the syntax or use case.

const chai = require('chai')
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const {expect} = chai

function p() {
  return new Promise( (resolve) => {
    setTimeout( () => {
      resolve(10);
    },5000);
  })
}

describe('Chai Eventually test', () => {
  it('should fail!', () => {
    expect(p()).to.eventually.equal(5)
  });
  it('should also fail!', async () => {
    expect(await p()).to.equal(5)
  });
})

running it with mocha I get:

Chai Eventually test
    ✔ should fail!
    1) should also fail!


  1 passing (2s)
  1 failing

  1) Chai Eventually test
       should also fail!:

      AssertionError: expected 10 to equal 5
      + expected - actual

      -10
      +5
      
      at Context.<anonymous> (chaiError.js:19:26)    
    
      1 passing (4ms)

This happens immediately so it is not waiting for the promise to resolve.

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

0

You have a couple options here.

The simplest is to just return the expect:

it('should fail!', () => {
  return expect(p()).to.eventually.equal(5); // <-- return this expectation
});

Alternatively, you can skip the return and use a done callback passed into chai's .notify method:

it('should fail using done!', (done) => { // <-- pass done into spec function
  expect(p()).to.eventually.equal(5).notify(done); // <-- pass done to .notify()
});

Documentation here.

And here's a StackBlitz showing it working correctly (i.e., failing) with those changes.

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