Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Promise-chains with Mocha

I've got the following style of function (using bluebird promises under Node.JS):

module.exports = {
    somefunc: Promise.method(function somefunc(v) {
        if (v.data === undefined)
            throw new Error("Expecting data");

        v.process_data = "xxx";

        return module.exports.someother1(v)
          .then(module.exports.someother2)
          .then(module.exports.someother3)
          .then(module.exports.someother4)
          .then(module.exports.someother5)
          .then(module.exports.someother6);
    }),
});

which I'm trying to test (using mocha, sinon, assert):

// our test subject
something = require('../lib/something');

describe('lib: something', function() {
    describe('somefunc', function() {
        it("should return a Error when called without data", function(done) {
            goterror = false;
            otherexception = false;
            something.somefunc({})
            .catch(function(expectedexception) {
                try {
                    assert.equal(expectedexception.message, 'Expecting data');
                } catch (unexpectedexception) {
                    otherexception = unexpectedexception;
                }
                goterror = true;
            })
            .finally(function(){
                if (otherexception)
                    throw otherexception;

                 assert(goterror);
                 done();
            });
        });
});

All of which works as such, but feels convoluted for one.

My MAIN problem is testing the Promises-chain (and order) in the function. I've tried several things (faking an object with a then method, which didn't work; mocking it like crazy, etc); but there seems to be something I'm not seeing, and I don't seem to be getting the mocha- or sinon- documentations in this regard.

Anybody got any pointers?

Thanks

count

like image 770
berlincount Avatar asked Dec 18 '14 09:12

berlincount


People also ask

How do you test your promise in Mocha?

If a function returns a promise that you want to assert on the result of the function, all you need to do for it to work effectively with Mocha is return the promise in your it block. Then you can assert on the result of the promise in the chained . then of your promise.

What is Mocha chai testing?

Mocha logo. Mocha is a JavaScript test framework running on Node. js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.


Video Answer


1 Answers

Mocha supports promises so you could do this

describe('lib: something', function() {
    describe('somefunc', function() {
        it("should return a Error when called without data", function() {
            return something.somefunc({})
                .then(assert.fail)
                .catch(function(e) {
                    assert.equal(e.message, "Expecting data");
                });
        });
    });
});

This is roughly equivalent to the synchronous code:

try {
   something.somefunc({});
   assert.fail();
} catch (e) {
   assert.equal(e.message, "Expecting data");
}
like image 140
Esailija Avatar answered Sep 30 '22 03:09

Esailija