Skip to content

Commit 7951747

Browse files
author
James Halliday
committed
nested test nearly works
1 parent 2a59c49 commit 7951747

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ function createHarness (conf_) {
3737
})(t);
3838

3939
results.push(t);
40-
nextTick(function () {
41-
t.run();
42-
});
40+
nextTick(function () { t.run() });
4341
return t;
4442
};
4543
test.stream = results;

lib/results.js

+47-36
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,27 @@ function Results (stream) {
3030
this.fail = 0;
3131
this.pass = 0;
3232
this.stream = stream;
33-
this.pending = 0;
33+
this.tests = [];
3434
}
3535

3636
Results.prototype.push = function (t) {
3737
var self = this;
3838
var write = function (s) { self.stream.queue(s) };
39-
write('# ' + t.name + '\n');
40-
self.pending ++;
39+
t.on('run', function () {
40+
write('# ' + t.name + '\n');
41+
});
42+
43+
var plan;
44+
t.on('plan', function (n) { plan = n });
4145

46+
var subtests = 0;
4247
t.on('test', function (st) {
48+
subtests ++;
49+
st.on('end', function () {
50+
nextTick(function () {
51+
if (--subtests === 0 && !plan) t.emit('end');
52+
});
53+
});
4354
self.push(st);
4455
});
4556

@@ -57,7 +68,9 @@ Results.prototype.push = function (t) {
5768

5869
t.on('end', function () {
5970
nextTick(function () {
60-
if (--self.pending === 0) self.close();
71+
if (subtests === 0) return;
72+
if (self.tests.length === 0) self.close();
73+
else self.tests.shift().run();
6174
});
6275
});
6376
};
@@ -84,49 +97,47 @@ function encodeResult (res, count) {
8497
else if (res.todo) output += ' # TODO';
8598

8699
output += '\n';
100+
if (res.ok) return output;
87101

88-
if (!res.ok) {
89-
var outer = ' ';
90-
var inner = outer + ' ';
91-
output += outer + '---\n';
92-
output += inner + 'operator: ' + res.operator + '\n';
93-
94-
var ex = json.stringify(res.expected, getSerialize()) || '';
95-
var ac = json.stringify(res.actual, getSerialize()) || '';
96-
97-
if (Math.max(ex.length, ac.length) > 65) {
98-
output += inner + 'expected:\n' + inner + ' ' + ex + '\n';
99-
output += inner + 'actual:\n' + inner + ' ' + ac + '\n';
100-
}
101-
else {
102-
output += inner + 'expected: ' + ex + '\n';
103-
output += inner + 'actual: ' + ac + '\n';
104-
}
105-
if (res.at) {
106-
output += inner + 'at: ' + res.at + '\n';
107-
}
108-
if (res.operator === 'error' && res.actual && res.actual.stack) {
109-
var lines = String(res.actual.stack).split('\n');
110-
output += inner + 'stack:\n';
111-
output += inner + ' ' + lines[0] + '\n';
112-
for (var i = 1; i < lines.length; i++) {
113-
output += inner + lines[i] + '\n';
114-
}
102+
var outer = ' ';
103+
var inner = outer + ' ';
104+
output += outer + '---\n';
105+
output += inner + 'operator: ' + res.operator + '\n';
106+
107+
var ex = json.stringify(res.expected, getSerialize()) || '';
108+
var ac = json.stringify(res.actual, getSerialize()) || '';
109+
110+
if (Math.max(ex.length, ac.length) > 65) {
111+
output += inner + 'expected:\n' + inner + ' ' + ex + '\n';
112+
output += inner + 'actual:\n' + inner + ' ' + ac + '\n';
113+
}
114+
else {
115+
output += inner + 'expected: ' + ex + '\n';
116+
output += inner + 'actual: ' + ac + '\n';
117+
}
118+
if (res.at) {
119+
output += inner + 'at: ' + res.at + '\n';
120+
}
121+
if (res.operator === 'error' && res.actual && res.actual.stack) {
122+
var lines = String(res.actual.stack).split('\n');
123+
output += inner + 'stack:\n';
124+
output += inner + ' ' + lines[0] + '\n';
125+
for (var i = 1; i < lines.length; i++) {
126+
output += inner + lines[i] + '\n';
115127
}
116-
117-
output += outer + '...\n';
118128
}
119-
129+
130+
output += outer + '...\n';
120131
return output;
121132
}
122133

123-
function getSerialize() {
134+
function getSerialize () {
124135
var seen = [];
125136

126137
return function (key, value) {
127138
var ret = value;
128139
if (typeof value === 'object' && value) {
129-
var found = false
140+
var found = false;
130141
for (var i = 0; i < seen.length; i++) {
131142
if (seen[i] === value) {
132143
found = true

0 commit comments

Comments
 (0)