Skip to content

Commit 7b05086

Browse files
committed
Handle children in order, and grandchildren
Fix #5 Fix #6
1 parent f86b1f5 commit 7b05086

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

index.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ function createHarness () {
1414
var out = new Render();
1515

1616
var test = function (name, conf, cb) {
17-
if (typeof conf === 'function') {
18-
cb = conf;
19-
conf = {};
20-
}
2117
var t = new Test(name, conf, cb);
2218

2319
process.nextTick(function () {
@@ -37,20 +33,26 @@ function createHarness () {
3733
else run();
3834
});
3935

40-
t.on('test', function (st) {
41-
pending.unshift(function () {
42-
running = true;
43-
out.push(st);
44-
st.run();
45-
46-
st.on('end', onend);
47-
});
36+
t.on('test', function sub (st) {
37+
st.on('test', sub);
38+
st.on('end', onend);
4839
});
4940

5041
t.on('end', onend);
5142

5243
function onend () {
5344
running = false;
45+
if (this._progeny.length) {
46+
var unshifts = this._progeny.map(function (st) {
47+
return function () {
48+
running = true;
49+
out.push(st);
50+
st.run();
51+
};
52+
});
53+
pending.unshift.apply(pending, unshifts);
54+
}
55+
5456
process.nextTick(function () {
5557
if (pending.length) pending.shift()()
5658
else out.close()

lib/test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function Test (name_, opts_, cb_) {
3131
this._skip = opts.skip || false;
3232
this._plan = undefined;
3333
this._cb = cb;
34+
this._progeny = [];
3435
}
3536

3637
Test.prototype.run = function () {
@@ -42,9 +43,9 @@ Test.prototype.run = function () {
4243
};
4344

4445
Test.prototype.test = function (name, opts, cb) {
45-
var self = this;
4646
var t = new Test(name, opts, cb);
47-
self.emit('test', t);
47+
this._progeny.push(t);
48+
this.emit('test', t);
4849
};
4950

5051
Test.prototype.comment = function (msg) {

test/child_ordering.js

+32
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,36 @@ test('uncle', function(t) {
1616
t.end();
1717
});
1818

19+
var grandParentRan = false;
20+
var parentRan = false;
21+
var grandChildRan = false;
22+
test('grandparent', function(t) {
23+
grandParentRan = true;
24+
t.test('parent', function(t) {
25+
parentRan = true;
26+
t.test('grandchild', function(t) {
27+
grandChildRan = true;
28+
t.pass('grand child ran');
29+
t.end();
30+
});
31+
t.pass('parent ran');
32+
t.end();
33+
});
34+
t.test('other parent', function(t) {
35+
t.ok(parentRan, 'first parent runs before second parent');
36+
t.ok(grandChildRan, 'grandchild runs before second parent');
37+
t.end();
38+
});
39+
t.pass('grandparent ran');
40+
t.end();
41+
});
42+
43+
test('second grandparent', function(t) {
44+
t.ok(grandParentRan, 'grandparent ran');
45+
t.ok(parentRan, 'parent ran');
46+
t.ok(grandChildRan, 'grandchild ran');
47+
t.pass('other grandparent ran');
48+
t.end();
49+
});
50+
1951
// vim: set softtabstop=4 shiftwidth=4:

0 commit comments

Comments
 (0)