node.js - Bluebird promises freeze when using Sinon's fake timer -


the following test freezes when used sinon's fake timers , bluebird.

var sinon = require('sinon'); var promise = require('bluebird');  describe('failing test', function() {   beforeeach(function() {     this.clock = sinon.usefaketimers();   });   aftereach(function() {     this.clock.restore();   });   it('test', function(done) {     promise.delay(1000).then(function(){         done(); //this never gets called          });   }); }); 

i using mocha (v2.2.5) bluebird (v2.9.33) , sinon (v1.15.3).

i tried suggestions offered in discussions in bluebird , sinon couldn't make work. seems issue way sinon stubs setimmediate other have no clue how resolve this.

you need step fake timer manually so:

describe('failing test', function() {   it('test', function(done) {     promise.delay(1000).then(function(){         done(); //this never gets called          });     //     // advance clock:     //     this.clock.tick(1000);   }); }); 

btw, mocha has built-in support promises, better way return promise:

describe('failing test', function() {   it('test', function() { // no more "done" callback     var p = promise.delay(1000).then(function(){         console.log('done');      });     this.clock.tick(1000);     return p; // return promise instead - mocha take of async magic goodness!   }); }); 

from experience, mixing promises , done callback style leads sorts of trouble , hard track errors. when using promises, try stick returning, , have @ library such chai-as-promised. promise it'll make tests more readable!


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -