Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

resolves #1066: generate better async trace if done() is called multiple times

Christopher Hiller1cfeb58bd99b
diff --git a/lib/runnable.js b/lib/runnable.js
index b0476f2f..0cb14a78 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -45,6 +45,7 @@ function Runnable(title, fn) {
this._slow = 75;
this._enableTimeouts = true;
this.timedOut = false;
+ this._trace = new Error('done() called multiple times')
}
/**
@@ -190,14 +191,14 @@ Runnable.prototype.run = function(fn){
function multiple(err) {
if (emitted) return;
emitted = true;
- self.emit('error', err || new Error('done() called multiple times'));
+ self.emit('error', err || new Error('done() called multiple times; stacktrace may be inaccurate'));
}
// finished
function done(err) {
var ms = self.timeout();
if (self.timedOut) return;
- if (finished) return multiple(err);
+ if (finished) return multiple(err || self._trace);
self.clearTimeout();
self.duration = new Date - start;
finished = true;
diff --git a/test/acceptance/multiple.done.js b/test/acceptance/multiple.done.js
index 65537af9..79874995 100644
--- a/test/acceptance/multiple.done.js
+++ b/test/acceptance/multiple.done.js
@@ -12,4 +12,12 @@ describe('multiple calls to done()', function(){
// done();
});
})
+
+ it('should produce a reasonable trace', function (done) {
+ process.nextTick(function() {
+ done();
+ // uncomment
+ // done()
+ })
+ });
})