Skip to content

Commit

Permalink
[Refactor] Avoid adding a new observable method to the API.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 30, 2019
1 parent e9036cb commit c083bce
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ var getTestArgs = function (name_, opts_, cb_) {
return { name: name, opts: opts, cb: cb };
};

var runProgeny = function () {
var self = this;
if (this._progeny.length) {
var t = this._progeny.shift();
t.on('end', function () { runProgeny.call(self) });
nextTick(function () {
t.run();
});
return;
}
if (this.calledEnd || this._plan) {
this._end();
}
};

function Test (name_, opts_, cb_) {
if (! (this instanceof Test)) {
return new Test(name_, opts_, cb_);
Expand Down Expand Up @@ -107,7 +122,7 @@ Test.prototype.test = function (name, opts, cb) {
});

if (!this._pendingAsserts()) {
this._runProgeny();
runProgeny.call(this);
}
};

Expand All @@ -123,14 +138,14 @@ Test.prototype.plan = function (n) {
this.emit('plan', n);
};

Test.prototype.timeoutAfter = function(ms) {
Test.prototype.timeoutAfter = function (ms) {
if (!ms) throw new Error('timeoutAfter requires a timespan');
var self = this;
var timeout = safeSetTimeout(function() {
var timeout = safeSetTimeout(function () {
self.fail('test timed out after ' + ms + 'ms');
self.end();
}, ms);
this.once('end', function() {
this.once('end', function () {
safeClearTimeout(timeout);
});
}
Expand All @@ -144,7 +159,7 @@ Test.prototype.end = function (err) {
this.fail('.end() called twice');
}
this.calledEnd = true;
this._runProgeny();
runProgeny.call(this);
};

Test.prototype._end = function (err) {
Expand All @@ -160,21 +175,6 @@ Test.prototype._end = function (err) {
this.ended = true;
};

Test.prototype._runProgeny = function () {
var self = this;
if (this._progeny.length) {
var t = this._progeny.shift();
t.on('end', function () { self._runProgeny() });
nextTick(function() {
t.run();
});
return;
}
if(this.calledEnd || this._plan) {
this._end();
}
};

Test.prototype._exit = function () {
if (this._plan !== undefined &&
!this._planError && this.assertCount !== this._plan) {
Expand Down Expand Up @@ -296,7 +296,7 @@ Test.prototype._assert = function assert (ok, opts) {
if (extra.exiting) {
self._end();
} else {
self._runProgeny();
runProgeny.call(self);
}
}

Expand Down

0 comments on commit c083bce

Please sign in to comment.