Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda