Skip to content

Commit 26c05e3

Browse files
author
James Halliday
committed
objectMode for stream output
1 parent 5d2cd63 commit 26c05e3

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

Diff for: index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ exports = module.exports = (function () {
2626
return getHarness.only.apply(this, arguments);
2727
}
2828

29-
lazyLoad.createStream = function () {
29+
lazyLoad.createStream = function (opts) {
30+
if (!opts) opts = {};
3031
if (!harness) {
3132
var output = through();
32-
getHarness({ stream: output });
33+
getHarness({ stream: output, objectMode: opts.objectMode });
3334
return output;
3435
}
35-
return harness.createStream();
36+
return harness.createStream(opts);
3637
};
3738

3839
return lazyLoad
@@ -51,7 +52,7 @@ function createExitHarness (conf) {
5152
autoclose: defined(conf.autoclose, false)
5253
});
5354

54-
var stream = harness.createStream();
55+
var stream = harness.createStream({ objectMode: conf.objectMode });
5556
var es = stream.pipe(conf.stream || createDefaultStream());
5657
if (canEmitExit) {
5758
es.on('error', function (err) { harness._exitCode = 1 });
@@ -124,8 +125,8 @@ function createHarness (conf_) {
124125

125126
test._tests = [];
126127

127-
test.createStream = function () {
128-
return results.createStream();
128+
test.createStream = function (opts) {
129+
return results.createStream(opts);
129130
};
130131

131132
var only = false;

Diff for: lib/results.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,43 @@ function Results () {
2020
this.tests = [];
2121
}
2222

23-
Results.prototype.createStream = function () {
23+
Results.prototype.createStream = function (opts) {
24+
if (!opts) opts = {};
2425
var self = this;
25-
var output = resumer();
26-
output.queue('TAP version 13\n');
26+
var output, testId = 0;
27+
if (opts.objectMode) {
28+
output = through();
29+
self.on('_push', function ontest (t, extra) {
30+
if (!extra) extra = {};
31+
var id = testId++;
32+
var row = {
33+
type: 'test',
34+
name: t.name,
35+
id: id
36+
};
37+
if (extra.parent) {
38+
row.parent = extra.parent;
39+
}
40+
output.queue(row);
41+
t.on('test', function (st) {
42+
ontest(st, { parent: id });
43+
});
44+
t.on('result', function (res) {
45+
res.test = id;
46+
res.type = 'assert';
47+
output.queue(res);
48+
});
49+
t.on('end', function () {
50+
output.queue({ type: 'end', test: id });
51+
});
52+
});
53+
self.on('done', function () { output.queue(null) });
54+
}
55+
else {
56+
output = resumer();
57+
output.queue('TAP version 13\n');
58+
self._stream.pipe(output);
59+
}
2760

2861
nextTick(function next() {
2962
var t;
@@ -33,7 +66,6 @@ Results.prototype.createStream = function () {
3366
}
3467
self.emit('done');
3568
});
36-
self._stream.pipe(output);
3769

3870
return output;
3971
};
@@ -42,6 +74,7 @@ Results.prototype.push = function (t) {
4274
var self = this;
4375
self.tests.push(t);
4476
self._watch(t);
77+
self.emit('_push', t);
4578
};
4679

4780
Results.prototype.only = function (name) {
@@ -154,7 +187,7 @@ function getSerialize () {
154187
};
155188
}
156189

157-
function getNextTest(results) {
190+
function getNextTest (results) {
158191
if (!results._only) {
159192
return results.tests.shift();
160193
}

0 commit comments

Comments
 (0)