Skip to content

Commit 48964a6

Browse files
grncdrJames Halliday
authored and
James Halliday
committed
Refactor test-ordering logic
Emit child tests to be run on successive calls to .end rather than explicitly ordering them as they are created.
1 parent cfff35b commit 48964a6

File tree

2 files changed

+19
-63
lines changed

2 files changed

+19
-63
lines changed

lib/results.js

+18-63
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ module.exports = function () {
2020

2121
nextTick(function next () {
2222
var t = getNextTest(results);
23-
if (!t && results.running) return;
24-
if (!t) return results.close();
25-
t.run();
23+
if (t) t.run();
24+
else results.close();
2625
});
2726

2827
return output;
@@ -34,52 +33,26 @@ function Results (stream) {
3433
this.pass = 0;
3534
this.stream = stream;
3635
this.tests = [];
37-
this.running = 0;
3836
}
3937

40-
Results.prototype.push = function (t, parentT) {
38+
Results.prototype.push = function (t) {
39+
var self = this;
40+
self.tests.push(t);
41+
self._watch(t);
42+
t.once('end', function () {
43+
var nt = getNextTest(self);
44+
if (nt) nt.run();
45+
else self.close();
46+
});
47+
};
48+
49+
Results.prototype._watch = function (t) {
4150
var self = this;
4251
var write = function (s) { self.stream.queue(s) };
4352
t.once('prerun', function () {
44-
if (self.only && self.only !== t.name && !parentT) {
45-
var nt = getNextTest(self);
46-
if (nt) nt.run()
47-
else self.close();
48-
return;
49-
}
50-
51-
self.running ++;
5253
write('# ' + t.name + '\n');
5354
});
54-
if (parentT) {
55-
var ix = self.tests.indexOf(parentT);
56-
if (ix >= 0) self.tests.splice(ix, 0, t);
57-
}
58-
else self.tests.push(t);
59-
60-
var plan;
61-
t.on('plan', function (n) { plan = n });
62-
63-
var subtests = 0;
64-
65-
t.on('test', function (st) {
66-
subtests ++;
67-
st.on('end', function () {
68-
subtests --;
69-
if (subtests === 1) nextTick(function () { st.run() });
70-
else if (subtests === 0 && !t.ended) {
71-
t.end();
72-
}
73-
});
74-
self.push(st, t);
75-
if (subtests === 1) {
76-
if (plan === undefined) st.run();
77-
else nextTick(function () {
78-
st.run();
79-
});
80-
}
81-
});
82-
55+
8356
t.on('result', function (res) {
8457
if (typeof res === 'string') {
8558
write('# ' + res + '\n');
@@ -91,27 +64,9 @@ Results.prototype.push = function (t, parentT) {
9164
if (res.ok) self.pass ++
9265
else self.fail ++
9366
});
94-
95-
t.once('end', function () {
96-
if (t._skip) {
97-
var nt = getNextTest(self);
98-
if (nt) nt.run();
99-
else self.close();
100-
return;
101-
}
102-
103-
self.running --;
104-
if (subtests !== 0) return;
105-
106-
if (self.running === 0 && self.tests.length) {
107-
var nt = getNextTest(self);
108-
if (nt) nt.run();
109-
else self.close();
110-
}
111-
else if (self.running === 0) {
112-
self.close();
113-
}
114-
});
67+
68+
t.on('test', function (st) { self._watch(st) });
69+
t.on('next', function (st) { st.run() });
11570
};
11671

11772
Results.prototype.close = function () {

lib/test.js

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Test.prototype.end = function () {
7979
if (this._progeny.length) {
8080
var t = this._progeny.shift();
8181
t.on('end', function () { self.end() });
82+
this.emit('next', t);
8283
return;
8384
}
8485

0 commit comments

Comments
 (0)