Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added protection against multiple calls to done(). Closes #35

Tj Holowaychukcd15f89e2f54
diff --git a/lib/runner.js b/lib/runner.js
index 2bfbe1c1..5c919e6c 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -162,12 +162,19 @@ Runner.prototype.hook = function(name, fn){
*/
Runner.prototype.runTest = function(fn){
- var test = this.test;
+ var test = this.test
+ , self = this;
// run the test
try {
// async
- if (test.async) return test.run(fn);
+ if (test.async) return test.run(function(err){
+ if (test.finished) {
+ self.fail(test, new Error('done() called multiple times'));
+ return;
+ }
+ fn(err);
+ });
// sync
process.nextTick(function(){
test.run();
diff --git a/lib/test.js b/lib/test.js
index d5ddafc0..41301996 100644
--- a/lib/test.js
+++ b/lib/test.js
@@ -73,6 +73,7 @@ Test.prototype.run = function(fn){
clearTimeout(timer);
self.duration = new Date - start;
fn(err);
+ self.finished = true;
});
// sync
} else {
diff --git a/test/unit/multiple.done.js b/test/unit/multiple.done.js
new file mode 100644
index 00000000..0425e7e7
--- /dev/null
+++ b/test/unit/multiple.done.js
@@ -0,0 +1,10 @@
+
+describe('multiple calls to done()', function(){
+ it('should fail in a test-case', function(done){
+ process.nextTick(function(){
+ done();
+ // uncomment
+ // done();
+ });
+ })
+})
\ No newline at end of file