|
1 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | 2 | import { nodeStackLineParser } from '../../../src/utils/node-stack-trace'; |
3 | | -import { stripSentryFramesAndReverse } from '../../../src/utils/stacktrace'; |
| 3 | +import { createStackParser, stripSentryFramesAndReverse } from '../../../src/utils/stacktrace'; |
4 | 4 |
|
5 | 5 | describe('Stacktrace', () => { |
| 6 | + describe('createStackParser()', () => { |
| 7 | + it('skips lines that contain "Error: " (e.g. "TypeError: foo")', () => { |
| 8 | + const mockParser = vi.fn().mockReturnValue({ filename: 'test.js', function: 'test', lineno: 1, colno: 1 }); |
| 9 | + const parser = createStackParser([0, mockParser]); |
| 10 | + |
| 11 | + const stack = ['TypeError: foo is not a function', ' at test (test.js:1:1)'].join('\n'); |
| 12 | + |
| 13 | + const frames = parser(stack); |
| 14 | + |
| 15 | + // The parser should only be called for the frame line, not the Error line |
| 16 | + expect(mockParser).toHaveBeenCalledTimes(1); |
| 17 | + expect(frames).toHaveLength(1); |
| 18 | + }); |
| 19 | + |
| 20 | + it('skips various Error type lines', () => { |
| 21 | + const mockParser = vi.fn().mockReturnValue({ filename: 'test.js', function: 'test', lineno: 1, colno: 1 }); |
| 22 | + const parser = createStackParser([0, mockParser]); |
| 23 | + |
| 24 | + const stack = [ |
| 25 | + 'Error: something went wrong', |
| 26 | + 'TypeError: foo is not a function', |
| 27 | + 'RangeError: Maximum call stack size exceeded', |
| 28 | + 'SomeCustomError: custom message', |
| 29 | + ' at test (test.js:1:1)', |
| 30 | + ].join('\n'); |
| 31 | + |
| 32 | + const frames = parser(stack); |
| 33 | + |
| 34 | + // Only the frame line should be parsed, all Error lines should be skipped |
| 35 | + expect(mockParser).toHaveBeenCalledTimes(1); |
| 36 | + expect(frames).toHaveLength(1); |
| 37 | + }); |
| 38 | + |
| 39 | + // Regression test for https://github.com/getsentry/sentry-javascript/issues/20052 |
| 40 | + it('processes long non-whitespace lines without hanging', () => { |
| 41 | + const mockParser = vi.fn().mockReturnValue(undefined); |
| 42 | + const parser = createStackParser([0, mockParser]); |
| 43 | + |
| 44 | + // Long non-whitespace lines (e.g. minified URLs) previously caused O(n²) backtracking |
| 45 | + const longLine = 'a'.repeat(2000); |
| 46 | + const stack = [longLine, ' at test (test.js:1:1)'].join('\n'); |
| 47 | + |
| 48 | + // Should complete without hanging (line gets truncated to 1024 chars internally) |
| 49 | + parser(stack); |
| 50 | + expect(mockParser).toHaveBeenCalledTimes(2); |
| 51 | + }); |
| 52 | + |
| 53 | + it('does not skip lines that do not contain "Error: "', () => { |
| 54 | + const mockParser = vi.fn().mockReturnValue({ filename: 'test.js', function: 'test', lineno: 1, colno: 1 }); |
| 55 | + const parser = createStackParser([0, mockParser]); |
| 56 | + |
| 57 | + const stack = [ |
| 58 | + ' at foo (test.js:1:1)', |
| 59 | + ' at bar (test.js:2:1)', |
| 60 | + 'ResizeObserver loop completed with undelivered notifications.', |
| 61 | + ].join('\n'); |
| 62 | + |
| 63 | + parser(stack); |
| 64 | + |
| 65 | + // All lines should be attempted by the parser (none contain "Error: ") |
| 66 | + expect(mockParser).toHaveBeenCalledTimes(3); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
6 | 70 | describe('stripSentryFramesAndReverse()', () => { |
7 | 71 | describe('removed top frame if its internally reserved word (public API)', () => { |
8 | 72 | it('reserved captureException', () => { |
|
0 commit comments