1

I am trying to test a piece of code I wrote with bluebird promises, by using chai-as-promised.

My method to be tested returns a promise rejection, as follows:

  /**
   * Creates a new user entry in the database.
   * @param {JSONObject} userData -- User details for creation.
   * @return => {boolean} true if succefully created, false otherwise.
   **/
  createUser: function(userData) {
    return Promise.reject();
  },

The testing code is as this:

    describe('User creation test suite', function() {
        it('Should successfully create root user', function(done) {
            expect(users.createUser(sampleUsers.raam))
                .to.eventually.have.property('id').and.notify(done);
          //expect(Promise.resolve({foo:'bar'})).to.eventually.have.property('id').and.notify(done);
        });

Eventhough, the method is intentionally failing, but the testcase is being marked as passed. Here is the output.

User creation test suite

  ✓ Should successfully create root user

If I test directly with a hardcoded string like

expect(Promise.resolve({foo:'bar'})).to.eventually.have.property('id').and.notify(done);

Then it seems to work. What am I doing wrong here?

EDIT I found that it had nothing todo with bluebird promises or my method which is under test. A simple hardcoded rejection also doesn't work correctly. That is...,

expect(Promise.reject({foo:'bar'})).to.eventually.have.property('id').and.notify(done)

returns as passed, instead of failing. Am I doing something terribly wrong here?

Community
  • 1
  • 1
Mopparthy Ravindranath
  • 3,014
  • 6
  • 41
  • 78

1 Answers1

0

Calling passing a callback to your Mocha test and then calling it when execution has finished -- as you are doing with the done callback -- is just one way to deal with asynchronous code in Mocha. Another is to simply return a promise from your test, which you can get from chai-as-promised. Try modifying you test like so:

it('Should successfully create root user', function() {
    return expect(users.createUser(sampleUsers.raam)).to.eventually.have.property('id');
});
Nathan Thompson
  • 2,354
  • 1
  • 23
  • 28