Search code examples
node.jspromisesequelize.jsmodelschai

getById() returns undefined Promise


I have the following class method in my Sequelize model:

getById(id) {
      return new Promise((resolve, reject) => {
          var Conference = sequelize.models.conference;
          Conference.findById(id).then(function(conference) {
              if (_.isObject(conference)) {
                  resolve(conference);
              } else {
                  throw new ResourceNotFound(conference.name, {id: id});
              }
          }).catch(function(err) {
              reject(err);
          });
      });
  }

Now I want to test my method with chai. But now when I do Conference.getById(confereceId) I get the following back:

Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }

Is this right and how do I assert the result of it with chai?


Solution

  • Your Conference.getById(confereceId) call returns a promise, so you should resolve the promise first via then, and assert the result of it with chai like this:

    const assert = require('chai').assert;
    
    Conference
      .getById(confereceId)
      .then(conf => {
        assert.isObject(conf);
        // ...other assertions
      }, err => {
        assert.isOk(false, 'conference not found!');
        // no conference found, fail assertion when it should be there
      });