Skip to content

Commit

Permalink
guard against calling .end() twice
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Mar 5, 2014
1 parent c0a9a29 commit 8d25028
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ function Test (name_, opts_, cb_) {

Test.prototype.run = function () {
if (!this._cb || this._skip) {
return this.end();
return this._end();
}
this.emit('prerun');
try {
this._cb(this);
}
catch (err) {
this.error(err);
this.end();
this._end();
return;
}
this.emit('run');
Expand All @@ -75,13 +75,13 @@ Test.prototype.test = function (name, opts, cb) {

if (!self._pendingAsserts()) {
nextTick(function () {
self.end();
self._end();
});
}

nextTick(function() {
if (!self._plan && self.pendingCount == self._progeny.length) {
self.end();
self._end();
}
});
};
Expand All @@ -95,16 +95,24 @@ Test.prototype.plan = function (n) {
this.emit('plan', n);
};

Test.prototype.end = function (err) {
Test.prototype.end = function (err) {
var self = this;
if (arguments.length === 1) {
if (arguments.length >= 1) {
this.ifError(err);
}

if (this.calledEnd) {
this.fail('.end() called twice');
}
this.calledEnd = true;
this._end();
};

Test.prototype._end = function (err) {
if (this._progeny.length) {
var t = this._progeny.shift();
t.on('end', function () {
self.end();
self._end();
});
t.run();
return;
Expand Down Expand Up @@ -199,10 +207,10 @@ Test.prototype._assert = function assert (ok, opts) {
var pendingAsserts = self._pendingAsserts();
if (!pendingAsserts) {
if (extra.exiting) {
self.end();
self._end();
} else {
nextTick(function () {
self.end();
self._end();
});
}
}
Expand Down

0 comments on commit 8d25028

Please sign in to comment.