mercredi 6 juillet 2016

Test return value from promise in chai

I have the following promises. I know they are working correctly, as the console.log output in funcThree returns the correct data. How do I prove this through testing?

Basically, how do I test this promise? I have tried to tests below, but no matter what I put in there (including expect(false).to.be.true), it always returns true. I do not believe it is actually reaching the expect statements.

CODE

let number = 0;
let count = 0;

// Find Mongoose document
function funcOne(query) {
  return new Promise(function (resolve, reject) {
    MongooseDocument.find(query)
      .exec(function (err, results) {
        if (err) {
          reject(err);
        }
        resolve(results);
      });
  });
}

// Iterate over each document and adjust external value
function funcTwo(documents) {
  return new Promise(function (resolve, reject) {
    _.each(documents, function (document) {
      count += 1;
      number += document.otherNumber;
    });
    if (count >= documents.length) {
      resolve({
        count,
        number,
      });
    }
  });
}

// Chain promises and return result
function funcThree(query) {
  return funcOne(query).then(funcTwo).then(function (result) {
    console.log("==================");
    console.log(result);
    return result;
  });
}

TEST EXAMPLE

// The expect test below never runs! How do I test this promise? Or
// better yet, how do I get the value returned as a result from the
// promise to test that?
it('should return an object', function () {
  funcThree(query).then(function(result) {
    return expect(result).to.be.an('object');
  });
});

Aucun commentaire:

Enregistrer un commentaire