Skip to content

Instantly share code, notes, and snippets.

@wujiang
Last active October 28, 2016 03:21
Show Gist options
  • Save wujiang/d51acc3c3ecd03ad2108f4ce3a1b1ca3 to your computer and use it in GitHub Desktop.
Save wujiang/d51acc3c3ecd03ad2108f4ce3a1b1ca3 to your computer and use it in GitHub Desktop.
mocha-3.1.2 fails
var async = require('async');
describe('timer test', function() {
it('should not report done() called multiple times', function(done) {
async.waterfall([
(next) => {
var timer = setTimeout(() => {
next(null, 'timer1');
}, 2);
// a mock function to run for 1 millisecond
setTimeout(() => {
clearTimeout(timer);
next(null, 'main 1');
}, 1);
},
(msg, next) => {
console.log(msg);
var timer = setTimeout(() => {
next(null, 'timer2');
}, 2);
setTimeout(() => {
clearTimeout(timer);
next(null, 'main 2');
}, 1);
}
], function(err, msg) {
done();
console.log(msg);
});
});
});
@wujiang
Copy link
Author

wujiang commented Oct 27, 2016

/tmp $ mocha


  timer test
timer1
    1) should not report done() called multiple times
    ✓ should not report done() called multiple times
timer2


  1 passing (51ms)
  1 failing

  1) timer test should not report done() called multiple times:
     Uncaught Error: Callback was already called.
      at node_modules/async/dist/async.js:985:32
      at null._onTimeout (test/test.js:12:21)




  1 passing (53ms)
  1 failing

  1) timer test should not report done() called multiple times:
     Uncaught Error: Callback was already called.
      at node_modules/async/dist/async.js:985:32
      at null._onTimeout (test/test.js:12:21)



/tmp $ mocha


  timer test
main 1
    ✓ should not report done() called multiple times (40ms)
timer2


  1 passing (50ms)

@wujiang
Copy link
Author

wujiang commented Oct 28, 2016

working version:

var async = require('async');

describe('timer test', function() {
    it('should not report done() called multiple times', function(done) {
        async.waterfall([
            (next) => {
                var needClear = true;
                var timer = setTimeout(() => {
                    needClear = false;
                    next(null, 'timer1');
                }, 2);

                // a mock function to run for 1 second
                setTimeout(() => {
                    if (needClear) {
                        clearTimeout(timer);
                        next(null, 'main 1');
                    }
                }, 1);
            },
            (msg, next) => {
                console.log(msg);
                var needClear = true;
                var timer = setTimeout(() => {
                    needClear = false;
                    next(null, 'timer2');
                }, 2);
                setTimeout(() => {
                    if (needClear) {
                        clearTimeout(timer);
                        next(null, 'main 2');
                    }
                }, 1);
            }
        ], function(err, msg) {
            done();
            console.log(msg);
        });
    });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment