samedi 10 septembre 2016

Node.js sinon test async.js waterfall function

Consider the following scenario:

function funcToTest() {
    async.waterfall([
        async.apply(otherFunc, 5, 'hi'),
        function(result, callback) {
            ...
            // I want to test this function
            // I can't move it to separate function due to closure variables used
        }
    ], function(err, result) {
           ...
        }
    );
}

function otherFunc(num, str) {
   ...
}

How would I go about testing the second function in the task array supplied to async.waterfall() ?
I've done the following but it doesn't work:

sinon.stub(module, 'otherFunc').yields(null, [1, 2, 3]);

but the second function is not being called and I'm not sure why. If I'm not mistaken asyn.waterfall() should call at-least its first task immediately, and I use sinon's yields() method so it will call the second task immediately after but it doesn't seem to work. My guess is that there's some asynchronous operation going behind the scene stalling the execution of the second task that I'm not aware of. I even tried use sinons fake timer after callingfuncToTest()` to see if the second task will be called after that, but it didn't work.

Aucun commentaire:

Enregistrer un commentaire