Skip to content

Commit

Permalink
Added protection against multiple calls to done(). Closes #35
Browse files Browse the repository at this point in the history
great suggestion by guillermo
  • Loading branch information
tj committed Nov 15, 2011
1 parent 561daf8 commit cd15f89
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Test.prototype.run = function(fn){
clearTimeout(timer);
self.duration = new Date - start;
fn(err);
self.finished = true;
});
// sync
} else {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/multiple.done.js
Original file line number Diff line number Diff line change
@@ -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();
});
})
})

1 comment on commit cd15f89

@rauchg
Copy link
Contributor

@rauchg rauchg commented on cd15f89 Nov 15, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pwnd

Please sign in to comment.