Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!