File: stackTrace.js

package info (click to toggle)
node-tape 4.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 636 kB
  • sloc: sh: 43; makefile: 20
file content (253 lines) | stat: -rw-r--r-- 7,100 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var tape = require('../');
var tap = require('tap');
var concat = require('concat-stream');
var tapParser = require('tap-parser');
var yaml = require('js-yaml');

tap.test('preserves stack trace with newlines', function (tt) {
    tt.plan(3);

    var test = tape.createHarness();
    var stream = test.createStream();
    var parser = stream.pipe(new tapParser());
    var stackTrace = 'foo\n  bar';

    parser.once('assert', function (data) {
        delete data.diag.at;
        tt.deepEqual(data, {
            ok: false,
            id: 1,
            name: "Error: Preserve stack",
            diag: {
                stack: stackTrace,
                operator: 'error',
                expected: 'undefined',
                actual: '[Error: Preserve stack]'
            }
        });
    });

    stream.pipe(concat(function (body) {
        var body = body.toString('utf8');
        body = stripAt(body);
        tt.equal(
            body,
            'TAP version 13\n'
            + '# multiline stack trace\n'
            + 'not ok 1 Error: Preserve stack\n'
            + '  ---\n'
            + '    operator: error\n'
            + '    expected: |-\n'
            + '      undefined\n'
            + '    actual: |-\n'
            + '      [Error: Preserve stack]\n'
            + '    stack: |-\n'
            + '      foo\n'
            + '        bar\n'
            + '  ...\n'
            + '\n'
            + '1..1\n'
            + '# tests 1\n'
            + '# pass  0\n'
            + '# fail  1\n'
        );

        tt.deepEqual(getDiag(body), {
            stack: stackTrace,
            operator: 'error',
            expected: 'undefined',
            actual: '[Error: Preserve stack]'
        });
    }));

    test('multiline stack trace', function (t) {
        t.plan(1);
        var err = new Error('Preserve stack');
        err.stack = stackTrace;
        t.error(err);
    });
});

tap.test('parses function name from original stack', function (tt) {
    tt.plan(1);

    var test = tape.createHarness();
    test.createStream();

    test._results._watch = function (t) {
        t.on('result', function (res) {
            tt.equal('Test.testFunctionNameParsing', res.functionName)
        });
    };

    test('t.equal stack trace', function testFunctionNameParsing(t) {
        t.equal(true, false, 'true should be false');
        t.end();
    });
});

tap.test('parses function name from original stack for anonymous function', function (tt) {
    tt.plan(1);

    var test = tape.createHarness();
    test.createStream();

    test._results._watch = function (t) {
        t.on('result', function (res) {
            tt.equal('Test.<anonymous>', res.functionName)
        });
    };

    test('t.equal stack trace', function (t) {
        t.equal(true, false, 'true should be false');
        t.end();
    });
});

tap.test('preserves stack trace for failed assertions', function (tt) {
    tt.plan(6);

    var test = tape.createHarness();
    var stream = test.createStream();
    var parser = stream.pipe(new tapParser());

    var stack = ''
    parser.once('assert', function (data) {
        tt.equal(typeof data.diag.at, 'string');
        tt.equal(typeof data.diag.stack, 'string');
        at = data.diag.at || '';
        stack = data.diag.stack || '';
        tt.ok(/^Error: true should be false(\n    at .+)+/.exec(stack), 'stack should be a stack')
        tt.deepEqual(data, {
            ok: false,
            id: 1,
            name: "true should be false",
            diag: {
                at: at,
                stack: stack,
                operator: 'equal',
                expected: false,
                actual: true
            }
        });
    });

    stream.pipe(concat(function (body) {
        var body = body.toString('utf8');
        body = stripAt(body);
        tt.equal(
            body,
            'TAP version 13\n'
            + '# t.equal stack trace\n'
            + 'not ok 1 true should be false\n'
            + '  ---\n'
            + '    operator: equal\n'
            + '    expected: false\n'
            + '    actual:   true\n'
            + '    stack: |-\n'
            + '      '
            + stack.replace(/\n/g, '\n      ') + '\n'
            + '  ...\n'
            + '\n'
            + '1..1\n'
            + '# tests 1\n'
            + '# pass  0\n'
            + '# fail  1\n'
        );

        tt.deepEqual(getDiag(body), {
            stack: stack,
            operator: 'equal',
            expected: false,
            actual: true
        });
    }));

    test('t.equal stack trace', function (t) {
        t.plan(1);
        t.equal(true, false, 'true should be false');
    });
});

tap.test('preserves stack trace for failed assertions where actual===falsy', function (tt) {
    tt.plan(6);

    var test = tape.createHarness();
    var stream = test.createStream();
    var parser = stream.pipe(new tapParser());

    var stack = ''
    parser.once('assert', function (data) {
        tt.equal(typeof data.diag.at, 'string');
        tt.equal(typeof data.diag.stack, 'string');
        at = data.diag.at || '';
        stack = data.diag.stack || '';
        tt.ok(/^Error: false should be true(\n    at .+)+/.exec(stack), 'stack should be a stack')
        tt.deepEqual(data, {
            ok: false,
            id: 1,
            name: "false should be true",
            diag: {
                at: at,
                stack: stack,
                operator: 'equal',
                expected: true,
                actual: false
            }
        });
    });

    stream.pipe(concat(function (body) {
        var body = body.toString('utf8');
        body = stripAt(body);
        tt.equal(
            body,
            'TAP version 13\n'
            + '# t.equal stack trace\n'
            + 'not ok 1 false should be true\n'
            + '  ---\n'
            + '    operator: equal\n'
            + '    expected: true\n'
            + '    actual:   false\n'
            + '    stack: |-\n'
            + '      '
            + stack.replace(/\n/g, '\n      ') + '\n'
            + '  ...\n'
            + '\n'
            + '1..1\n'
            + '# tests 1\n'
            + '# pass  0\n'
            + '# fail  1\n'
        );

        tt.deepEqual(getDiag(body), {
            stack: stack,
            operator: 'equal',
            expected: true,
            actual: false
        });
    }));

    test('t.equal stack trace', function (t) {
        t.plan(1);
        t.equal(false, true, 'false should be true');
    });
});

function getDiag (body) {
    var yamlStart = body.indexOf('  ---');
    var yamlEnd = body.indexOf('  ...\n');
    var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
        return line.slice(2);
    }).join('\n');

    // Get rid of 'at' variable (which has a line number / path of its own that's
    // difficult to check).
    var withStack = yaml.safeLoad(diag);
    delete withStack.at;
    return withStack;
}

function stripAt (body) {
    return body.replace(/^\s*at:\s+Test.*$\n/m, '');
}