Skip to content

Commit

Permalink
Refactor test-ordering logic
Browse files Browse the repository at this point in the history
Emit child tests to be run on successive calls to .end rather than explicitly
ordering them as they are created.
  • Loading branch information
grncdr authored and James Halliday committed Oct 25, 2013
1 parent cfff35b commit 48964a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 63 deletions.
81 changes: 18 additions & 63 deletions lib/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ module.exports = function () {

nextTick(function next () {
var t = getNextTest(results);
if (!t && results.running) return;
if (!t) return results.close();
t.run();
if (t) t.run();
else results.close();
});

return output;
Expand All @@ -34,52 +33,26 @@ function Results (stream) {
this.pass = 0;
this.stream = stream;
this.tests = [];
this.running = 0;
}

Results.prototype.push = function (t, parentT) {
Results.prototype.push = function (t) {
var self = this;
self.tests.push(t);
self._watch(t);
t.once('end', function () {
var nt = getNextTest(self);
if (nt) nt.run();
else self.close();
});
};

Results.prototype._watch = function (t) {
var self = this;
var write = function (s) { self.stream.queue(s) };
t.once('prerun', function () {
if (self.only && self.only !== t.name && !parentT) {
var nt = getNextTest(self);
if (nt) nt.run()
else self.close();
return;
}

self.running ++;
write('# ' + t.name + '\n');
});
if (parentT) {
var ix = self.tests.indexOf(parentT);
if (ix >= 0) self.tests.splice(ix, 0, t);
}
else self.tests.push(t);

var plan;
t.on('plan', function (n) { plan = n });

var subtests = 0;

t.on('test', function (st) {
subtests ++;
st.on('end', function () {
subtests --;
if (subtests === 1) nextTick(function () { st.run() });
else if (subtests === 0 && !t.ended) {
t.end();
}
});
self.push(st, t);
if (subtests === 1) {
if (plan === undefined) st.run();
else nextTick(function () {
st.run();
});
}
});


t.on('result', function (res) {
if (typeof res === 'string') {
write('# ' + res + '\n');
Expand All @@ -91,27 +64,9 @@ Results.prototype.push = function (t, parentT) {
if (res.ok) self.pass ++
else self.fail ++
});

t.once('end', function () {
if (t._skip) {
var nt = getNextTest(self);
if (nt) nt.run();
else self.close();
return;
}

self.running --;
if (subtests !== 0) return;

if (self.running === 0 && self.tests.length) {
var nt = getNextTest(self);
if (nt) nt.run();
else self.close();
}
else if (self.running === 0) {
self.close();
}
});

t.on('test', function (st) { self._watch(st) });
t.on('next', function (st) { st.run() });
};

Results.prototype.close = function () {
Expand Down
1 change: 1 addition & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Test.prototype.end = function () {
if (this._progeny.length) {
var t = this._progeny.shift();
t.on('end', function () { self.end() });
this.emit('next', t);
return;
}

Expand Down

0 comments on commit 48964a6

Please sign in to comment.