|
1 | 1 | var tap = require("tap"); |
2 | 2 | var tape = require("../"); |
3 | | -var trim = require('string.prototype.trim'); |
| 3 | +var concat = require('concat-stream'); |
4 | 4 |
|
5 | 5 | tap.test("tape assert.end as callback", function (tt) { |
6 | 6 | 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 | | - ]) |
31 | | - |
| 7 | + |
| 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'); |
32 | 25 | tt.end() |
33 | | - }) |
34 | | - |
35 | | - test.createStream().pipe(tc) |
| 26 | + })); |
36 | 27 |
|
37 | 28 | test("do a task and write", function (assert) { |
38 | 29 | fakeAsyncTask("foo", function (err, value) { |
@@ -64,3 +55,33 @@ function fakeAsyncWrite(name, cb) { |
64 | 55 | function fakeAsyncWriteFail(name, cb) { |
65 | 56 | cb(new Error("fail")) |
66 | 57 | } |
| 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