laiboonh
laiboonh

Reputation: 1457

Chai-As-Promised passes even when it is wrong

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

describe('[sample unit]', function() {
  it('should pass functionToTest with true input', function() {   
    expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh");
  });
});

This test passes??? I am using "chai": "3.5.0", "chai-as-promised": "5.2.0",

Upvotes: 4

Views: 732

Answers (1)

robertklep
robertklep

Reputation: 203359

expect(...) returns a promise itself, which will either be resolved or rejected, depending on the test.

For Mocha to test the outcome of that promise, you need to explicitly return it from the test case (this works because Mocha has built-in promise support):

describe('[sample unit]', function() {
  it('should pass functionToTest with true input', function() {   
    return expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh");
  });
});

Alternatively, you can use Mocha's "regular" callback-style async setup and chai-as-promised's .notify():

describe('[sample unit]', function() {
  it('should pass functionToTest with true input', function(done) {   
    expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh").notify(done);
  });
});

Upvotes: 7

Related Questions