Skip to content

Commit be10880

Browse files
committed
update test/end-as-callback.js to use concat-stream instead of tap.createCosumer (method unavailable in tap v7) for #312
1 parent 78e4ffd commit be10880

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed

test/end-as-callback.js

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
var tap = require("tap");
22
var tape = require("../");
3-
var trim = require('string.prototype.trim');
3+
var concat = require('concat-stream');
44

55
tap.test("tape assert.end as callback", function (tt) {
66
var test = tape.createHarness({ exit: false })
7-
var tc = tap.createConsumer()
8-
9-
var rows = []
10-
tc.on("data", function (r) { rows.push(r) })
11-
tc.on("end", function () {
12-
var rs = rows.map(function (r) {
13-
return r && typeof r === "object" ?
14-
{ id: r.id, ok: r.ok, name: trim(r.name) } :
15-
r
16-
})
17-
18-
tt.deepEqual(rs, [
19-
"TAP version 13",
20-
"do a task and write",
21-
{ id: 1, ok: true, name: "null" },
22-
{ id: 2, ok: true, name: "should be equal" },
23-
"do a task and write fail",
24-
{ id: 3, ok: true, name: "null" },
25-
{ id: 4, ok: true, name: "should be equal" },
26-
{ id: 5, ok: false, name: "Error: fail" },
27-
"tests 5",
28-
"pass 4",
29-
"fail 1"
30-
])
317

8+
test.createStream().pipe(concat(function (rows) {
9+
tt.equal(rows.toString('utf8'), [
10+
'TAP version 13',
11+
'# do a task and write',
12+
'ok 1 null',
13+
'ok 2 should be equal',
14+
'# do a task and write fail',
15+
'ok 3 null',
16+
'ok 4 should be equal',
17+
'not ok 5 Error: fail',
18+
getStackTrace(rows), // tap error stack
19+
'',
20+
'1..5',
21+
'# tests 5',
22+
'# pass 4',
23+
'# fail 1'
24+
].join('\n') + '\n');
3225
tt.end()
33-
})
34-
35-
test.createStream().pipe(tc)
26+
}));
3627

3728
test("do a task and write", function (assert) {
3829
fakeAsyncTask("foo", function (err, value) {
@@ -64,3 +55,33 @@ function fakeAsyncWrite(name, cb) {
6455
function fakeAsyncWriteFail(name, cb) {
6556
cb(new Error("fail"))
6657
}
58+
59+
/**
60+
* extract the stack trace for the failed test.
61+
* this will change dependent on the environment
62+
* so no point hard-coding it in the test assertion
63+
* see: https://git.io/v6hGG for example
64+
* @param String rows - the tap output lines
65+
* @returns String stacktrace - just the error stack part
66+
*/
67+
function getStackTrace(rows) {
68+
var stacktrace = ' ---\n';
69+
var extract = false;
70+
rows.toString('utf8').split('\n').forEach(function (row) {
71+
if (!extract) {
72+
if (row.indexOf('---') > -1) { // start of stack trace
73+
extract = true;
74+
}
75+
} else {
76+
if (row.indexOf('...') > -1) { // end of stack trace
77+
extract = false;
78+
stacktrace += ' ...';
79+
} else {
80+
stacktrace += row + '\n';
81+
}
82+
83+
}
84+
});
85+
// console.log(stacktrace);
86+
return stacktrace;
87+
}

0 commit comments

Comments
 (0)