-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathstacktrace.test.ts
383 lines (316 loc) · 12.3 KB
/
stacktrace.test.ts
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { nodeStackLineParser } from '../../src/utils-hoist/node-stack-trace';
import { stripSentryFramesAndReverse } from '../../src/utils-hoist/stacktrace';
describe('Stacktrace', () => {
describe('stripSentryFramesAndReverse()', () => {
describe('removed top frame if its internally reserved word (public API)', () => {
it('reserved captureException', () => {
const stack = [
{ colno: 1, lineno: 4, filename: 'anything.js', function: 'captureException' },
{ colno: 1, lineno: 3, filename: 'anything.js', function: 'foo' },
{ colno: 1, lineno: 2, filename: 'anything.js', function: 'bar' },
];
// Should remove `captureException` as its a name considered "internal"
const frames = stripSentryFramesAndReverse(stack);
expect(frames.length).toBe(2);
expect(frames[0]?.function).toBe('bar');
expect(frames[1]?.function).toBe('foo');
});
it('reserved captureMessage', () => {
const stack = [
{ colno: 1, lineno: 4, filename: 'anything.js', function: 'captureMessage' },
{ colno: 1, lineno: 3, filename: 'anything.js', function: 'foo' },
{ colno: 1, lineno: 2, filename: 'anything.js', function: 'bar' },
];
// Should remove `captureMessage` as its a name considered "internal"
const frames = stripSentryFramesAndReverse(stack);
expect(frames.length).toBe(2);
expect(frames[0]?.function).toBe('bar');
expect(frames[1]?.function).toBe('foo');
});
it('remove two occurences if they are present', () => {
const exceptionStack = [
{ colno: 1, lineno: 4, filename: 'anything.js', function: 'captureException' },
{ colno: 1, lineno: 4, filename: 'anything.js', function: 'captureException' },
{ colno: 1, lineno: 3, filename: 'anything.js', function: 'foo' },
{ colno: 1, lineno: 2, filename: 'anything.js', function: 'bar' },
];
const exceptionFrames = stripSentryFramesAndReverse(exceptionStack);
expect(exceptionFrames.length).toBe(2);
expect(exceptionFrames[0]?.function).toBe('bar');
expect(exceptionFrames[1]?.function).toBe('foo');
const messageStack = [
{ colno: 1, lineno: 4, filename: 'anything.js', function: 'captureMessage' },
{ colno: 1, lineno: 4, filename: 'anything.js', function: 'captureMessage' },
{ colno: 1, lineno: 3, filename: 'anything.js', function: 'foo' },
{ colno: 1, lineno: 2, filename: 'anything.js', function: 'bar' },
];
const messageFrames = stripSentryFramesAndReverse(messageStack);
expect(messageFrames.length).toBe(2);
expect(messageFrames[0]?.function).toBe('bar');
expect(messageFrames[1]?.function).toBe('foo');
});
});
describe('removed bottom frame if its internally reserved word (internal API)', () => {
it('reserved sentryWrapped', () => {
const stack = [
{ colno: 1, lineno: 3, filename: 'anything.js', function: 'foo' },
{ colno: 1, lineno: 2, filename: 'anything.js', function: 'bar' },
{ colno: 1, lineno: 1, filename: 'anything.js', function: 'sentryWrapped' },
];
// Should remove `sentryWrapped` as its a name considered "internal"
const frames = stripSentryFramesAndReverse(stack);
expect(frames.length).toBe(2);
expect(frames[0]?.function).toBe('bar');
expect(frames[1]?.function).toBe('foo');
});
});
it('removed top and bottom frame if they are internally reserved words', () => {
const stack = [
{ colno: 1, lineno: 4, filename: 'anything.js', function: 'captureMessage' },
{ colno: 1, lineno: 4, filename: 'anything.js', function: 'captureMessage' },
{ colno: 1, lineno: 3, filename: 'anything.js', function: 'foo' },
{ colno: 1, lineno: 2, filename: 'anything.js', function: 'bar' },
{ colno: 1, lineno: 1, filename: 'anything.js', function: 'sentryWrapped' },
];
// Should remove `captureMessage` and `sentryWrapped` as its a name considered "internal"
const frames = stripSentryFramesAndReverse(stack);
expect(frames.length).toBe(2);
expect(frames[0]?.function).toBe('bar');
expect(frames[1]?.function).toBe('foo');
});
it('applies frames limit after the stripping, not before', () => {
const stack = Array.from({ length: 55 }).map((_, i) => {
return { colno: 1, lineno: 4, filename: 'anything.js', function: `${i}` };
});
stack.unshift({ colno: 1, lineno: 4, filename: 'anything.js', function: 'captureMessage' });
stack.unshift({ colno: 1, lineno: 4, filename: 'anything.js', function: 'captureMessage' });
stack.push({ colno: 1, lineno: 4, filename: 'anything.js', function: 'sentryWrapped' });
// Should remove 2x `captureMessage`, `sentryWrapped`, and then limit frames to default 50.
const frames = stripSentryFramesAndReverse(stack);
expect(frames.length).toBe(50);
// Frames are named 0-54, thus after reversal and trimming, we should have frames 54-5, 50 in total.
expect(frames[0]?.function).toBe('54');
expect(frames[49]?.function).toBe('5');
});
});
});
describe('node', () => {
const mockGetModule = vi.fn();
const parser = nodeStackLineParser(mockGetModule);
const node = parser[1];
beforeEach(() => {
mockGetModule.mockReset();
});
it('should return undefined for invalid input', () => {
expect(node('invalid input')).toBeUndefined();
});
it('should extract function, module, filename, lineno, colno, and in_app from valid input', () => {
const input = 'at myFunction (/path/to/file.js:10:5)';
const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: 'myFunction',
lineno: 10,
colno: 5,
in_app: true,
};
expect(node(input)).toEqual(expectedOutput);
});
it('extracts module from getModule', () => {
const input = 'at myFunction (/path/to/file.js:10:5)';
mockGetModule.mockReturnValue('myModule');
expect(node(input)?.module).toEqual('myModule');
});
it('should extract anonymous function name correctly', () => {
const input = 'at /path/to/file.js:10:5';
const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: '?',
lineno: 10,
colno: 5,
in_app: true,
};
expect(node(input)).toEqual(expectedOutput);
});
it('should extract method name and type name correctly', () => {
const input = 'at myObject.myMethod (/path/to/file.js:10:5)';
const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: 'myObject.myMethod',
lineno: 10,
colno: 5,
in_app: true,
};
expect(node(input)).toEqual(expectedOutput);
});
it('should handle input with file:// protocol', () => {
const input = 'at myFunction (file:///path/to/file.js:10:5)';
const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: 'myFunction',
lineno: 10,
colno: 5,
in_app: true,
};
expect(node(input)).toEqual(expectedOutput);
});
it('should handle input with no line or column number', () => {
const input = 'at myFunction (/path/to/file.js)';
const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: 'myFunction',
lineno: undefined,
colno: undefined,
in_app: true,
};
expect(node(input)).toEqual(expectedOutput);
});
it('should handle input with "native" flag', () => {
const input = 'at myFunction (native)';
const expectedOutput = {
filename: undefined,
module: undefined,
function: 'myFunction',
lineno: undefined,
colno: undefined,
in_app: false,
};
expect(node(input)).toEqual(expectedOutput);
});
it('should correctly parse a stack trace line with a function name and file URL', () => {
const line = 'at myFunction (file:///path/to/myFile.js:10:20)';
const result = node(line);
expect(result).toEqual({
filename: '/path/to/myFile.js',
function: 'myFunction',
lineno: 10,
colno: 20,
in_app: true,
});
});
it('should correctly parse a stack trace line with a method name and filename', () => {
const line = 'at MyClass.myMethod (/path/to/myFile.js:10:20)';
const result = node(line);
expect(result).toEqual({
filename: '/path/to/myFile.js',
module: undefined,
function: 'MyClass.myMethod',
lineno: 10,
colno: 20,
in_app: true,
});
});
it('should correctly parse a stack trace line with an anonymous function', () => {
const line = 'at Object.<anonymous> (/path/to/myFile.js:10:20)';
const result = node(line);
expect(result).toEqual({
filename: '/path/to/myFile.js',
function: 'Object.?',
lineno: 10,
colno: 20,
in_app: true,
});
});
it('should correctly parse a stack trace line with no function or filename', () => {
const line = 'at /path/to/myFile.js:10:20';
const result = node(line);
expect(result).toEqual({
filename: '/path/to/myFile.js',
function: '?',
lineno: 10,
colno: 20,
in_app: true,
});
});
it('should correctly parse a stack trace line with a native function', () => {
const line = 'at Object.<anonymous> (native)';
const result = node(line);
expect(result).toEqual({
filename: undefined,
function: 'Object.?',
lineno: undefined,
colno: undefined,
in_app: false,
});
});
it('should correctly parse a stack trace line with a module filename', () => {
const line = 'at Object.<anonymous> (/path/to/node_modules/myModule/index.js:10:20)';
const result = node(line);
expect(result).toEqual({
filename: '/path/to/node_modules/myModule/index.js',
function: 'Object.?',
lineno: 10,
colno: 20,
in_app: false,
});
});
it('should correctly parse a stack trace line with a Windows filename', () => {
const line = 'at Object.<anonymous> (C:\\path\\to\\myFile.js:10:20)';
const result = node(line);
expect(result).toEqual({
filename: 'C:\\path\\to\\myFile.js',
function: 'Object.?',
lineno: 10,
colno: 20,
in_app: true,
});
});
it('should mark frames with protocols as in_app: true', () => {
const line = 'at Object.<anonymous> (app:///_next/server/pages/[error].js:10:20)';
const result = node(line);
expect(result?.in_app).toBe(true);
});
it('parses frame filename paths with spaces and characters in file name', () => {
const input = 'at myObject.myMethod (/path/to/file with space(1).js:10:5)';
const expectedOutput = {
filename: '/path/to/file with space(1).js',
module: undefined,
function: 'myObject.myMethod',
lineno: 10,
colno: 5,
in_app: true,
};
expect(node(input)).toEqual(expectedOutput);
});
it('parses frame filename paths with spaces and characters in file path', () => {
const input = 'at myObject.myMethod (/path with space(1)/to/file.js:10:5)';
const expectedOutput = {
filename: '/path with space(1)/to/file.js',
module: undefined,
function: 'myObject.myMethod',
lineno: 10,
colno: 5,
in_app: true,
};
expect(node(input)).toEqual(expectedOutput);
});
it('parses encoded frame filename paths with spaces and characters in file name', () => {
const input = 'at myObject.myMethod (/path/to/file%20with%20space(1).js:10:5)';
const expectedOutput = {
filename: '/path/to/file with space(1).js',
module: undefined,
function: 'myObject.myMethod',
lineno: 10,
colno: 5,
in_app: true,
};
expect(node(input)).toEqual(expectedOutput);
});
it('parses encoded frame filename paths with spaces and characters in file path', () => {
const input = 'at myObject.myMethod (/path%20with%20space(1)/to/file.js:10:5)';
const expectedOutput = {
filename: '/path with space(1)/to/file.js',
module: undefined,
function: 'myObject.myMethod',
lineno: 10,
colno: 5,
in_app: true,
};
expect(node(input)).toEqual(expectedOutput);
});
});