forked from tape-testing/tape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathend-as-callback.js
88 lines (77 loc) · 2.27 KB
/
end-as-callback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var tap = require("tap");
var tape = require("../");
var concat = require('concat-stream');
tap.test("tape assert.end as callback", function (tt) {
var test = tape.createHarness({ exit: false })
test.createStream().pipe(concat(function (rows) {
tt.equal(rows.toString('utf8'), [
'TAP version 13',
'# do a task and write',
'ok 1 null',
'ok 2 should be equal',
'# do a task and write fail',
'ok 3 null',
'ok 4 should be equal',
'not ok 5 Error: fail',
getStackTrace(rows), // tap error stack
'',
'1..5',
'# tests 5',
'# pass 4',
'# fail 1',
''
].join('\n'));
tt.end()
}));
test("do a task and write", function (assert) {
fakeAsyncTask("foo", function (err, value) {
assert.ifError(err)
assert.equal(value, "taskfoo")
fakeAsyncWrite("bar", assert.end)
})
})
test("do a task and write fail", function (assert) {
fakeAsyncTask("bar", function (err, value) {
assert.ifError(err)
assert.equal(value, "taskbar")
fakeAsyncWriteFail("baz", assert.end)
})
})
})
function fakeAsyncTask(name, cb) {
cb(null, "task" + name)
}
function fakeAsyncWrite(name, cb) {
cb(null)
}
function fakeAsyncWriteFail(name, cb) {
cb(new Error("fail"))
}
/**
* extract the stack trace for the failed test.
* this will change dependent on the environment
* so no point hard-coding it in the test assertion
* see: https://git.io/v6hGG for example
* @param String rows - the tap output lines
* @returns String stacktrace - just the error stack part
*/
function getStackTrace(rows) {
var stacktrace = ' ---\n';
var extract = false;
rows.toString('utf8').split('\n').forEach(function (row) {
if (!extract) {
if (row.indexOf('---') > -1) { // start of stack trace
extract = true;
}
} else {
if (row.indexOf('...') > -1) { // end of stack trace
extract = false;
stacktrace += ' ...';
} else {
stacktrace += row + '\n';
}
}
});
// console.log(stacktrace);
return stacktrace;
}