Search code examples
javascriptnode.jsunit-testingchai

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.


Solution

  • 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.