|
| 1 | +// Copyright 2026 The Chromium Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// eslint-disable-next-line @devtools/es-modules-import |
| 6 | +import * as StackTraceImpl from './stack_trace_impl.js'; |
| 7 | + |
| 8 | +describe('DetailedErrorStackParser', () => { |
| 9 | + describe('parseRawFramesFromErrorStack', () => { |
| 10 | + it('parses standard V8 stack frames', () => { |
| 11 | + const stack = `Error: foo |
| 12 | + at functionName (http://www.example.org/script.js:10:5) |
| 13 | + at Class.methodName (http://www.example.org/script.js:20:1) |
| 14 | + at new Constructor (http://www.example.org/script.js:30:1) |
| 15 | + at async asyncFunction (http://www.example.org/script.js:40:1) |
| 16 | + at http://www.example.org/script.js:50:1`; |
| 17 | + const frames = StackTraceImpl.DetailedErrorStackParser.parseRawFramesFromErrorStack(stack); |
| 18 | + |
| 19 | + assert.lengthOf(frames, 5); |
| 20 | + assert.deepEqual(frames[0], { |
| 21 | + url: 'http://www.example.org/script.js', |
| 22 | + functionName: 'functionName', |
| 23 | + lineNumber: 9, |
| 24 | + columnNumber: 4, |
| 25 | + parsedFrameInfo: { |
| 26 | + isAsync: false, |
| 27 | + isConstructor: false, |
| 28 | + isEval: false, |
| 29 | + isWasm: false, |
| 30 | + wasmModuleName: undefined, |
| 31 | + wasmFunctionIndex: undefined, |
| 32 | + typeName: undefined, |
| 33 | + methodName: undefined, |
| 34 | + promiseIndex: undefined, |
| 35 | + evalOrigin: undefined, |
| 36 | + }, |
| 37 | + }); |
| 38 | + assert.deepEqual(frames[1], { |
| 39 | + url: 'http://www.example.org/script.js', |
| 40 | + functionName: 'Class.methodName', |
| 41 | + lineNumber: 19, |
| 42 | + columnNumber: 0, |
| 43 | + parsedFrameInfo: { |
| 44 | + isAsync: false, |
| 45 | + isConstructor: false, |
| 46 | + isEval: false, |
| 47 | + isWasm: false, |
| 48 | + wasmModuleName: undefined, |
| 49 | + wasmFunctionIndex: undefined, |
| 50 | + typeName: 'Class', |
| 51 | + methodName: 'methodName', |
| 52 | + promiseIndex: undefined, |
| 53 | + evalOrigin: undefined, |
| 54 | + }, |
| 55 | + }); |
| 56 | + assert.deepEqual(frames[2], { |
| 57 | + url: 'http://www.example.org/script.js', |
| 58 | + functionName: 'Constructor', |
| 59 | + lineNumber: 29, |
| 60 | + columnNumber: 0, |
| 61 | + parsedFrameInfo: { |
| 62 | + isAsync: false, |
| 63 | + isConstructor: true, |
| 64 | + isEval: false, |
| 65 | + isWasm: false, |
| 66 | + wasmModuleName: undefined, |
| 67 | + wasmFunctionIndex: undefined, |
| 68 | + typeName: undefined, |
| 69 | + methodName: undefined, |
| 70 | + promiseIndex: undefined, |
| 71 | + evalOrigin: undefined, |
| 72 | + }, |
| 73 | + }); |
| 74 | + assert.deepEqual(frames[3], { |
| 75 | + url: 'http://www.example.org/script.js', |
| 76 | + functionName: 'asyncFunction', |
| 77 | + lineNumber: 39, |
| 78 | + columnNumber: 0, |
| 79 | + parsedFrameInfo: { |
| 80 | + isAsync: true, |
| 81 | + isConstructor: false, |
| 82 | + isEval: false, |
| 83 | + isWasm: false, |
| 84 | + wasmModuleName: undefined, |
| 85 | + wasmFunctionIndex: undefined, |
| 86 | + typeName: undefined, |
| 87 | + methodName: undefined, |
| 88 | + promiseIndex: undefined, |
| 89 | + evalOrigin: undefined, |
| 90 | + }, |
| 91 | + }); |
| 92 | + assert.deepEqual(frames[4], { |
| 93 | + url: 'http://www.example.org/script.js', |
| 94 | + functionName: '', |
| 95 | + lineNumber: 49, |
| 96 | + columnNumber: 0, |
| 97 | + parsedFrameInfo: { |
| 98 | + isAsync: false, |
| 99 | + isConstructor: false, |
| 100 | + isEval: false, |
| 101 | + isWasm: false, |
| 102 | + wasmModuleName: undefined, |
| 103 | + wasmFunctionIndex: undefined, |
| 104 | + typeName: undefined, |
| 105 | + methodName: undefined, |
| 106 | + promiseIndex: undefined, |
| 107 | + evalOrigin: undefined, |
| 108 | + }, |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('parses eval frames', () => { |
| 113 | + const stack = `Error: foo |
| 114 | + at eval (eval at <anonymous> (http://www.example.org/script.js:10:5), <anonymous>:1:1)`; |
| 115 | + const frames = StackTraceImpl.DetailedErrorStackParser.parseRawFramesFromErrorStack(stack); |
| 116 | + |
| 117 | + assert.lengthOf(frames, 1); |
| 118 | + assert.isTrue(frames[0].parsedFrameInfo?.isEval); |
| 119 | + assert.strictEqual(frames[0].url, '<anonymous>'); |
| 120 | + assert.strictEqual(frames[0].lineNumber, 0); |
| 121 | + assert.strictEqual(frames[0].columnNumber, 0); |
| 122 | + |
| 123 | + assert.exists(frames[0].parsedFrameInfo?.evalOrigin); |
| 124 | + assert.strictEqual(frames[0].parsedFrameInfo?.evalOrigin?.url, 'http://www.example.org/script.js'); |
| 125 | + assert.strictEqual(frames[0].parsedFrameInfo?.evalOrigin?.lineNumber, 9); |
| 126 | + assert.strictEqual(frames[0].parsedFrameInfo?.evalOrigin?.columnNumber, 4); |
| 127 | + assert.strictEqual(frames[0].parsedFrameInfo?.evalOrigin?.functionName, '<anonymous>'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('parses deeply nested eval frames with actual function names', () => { |
| 131 | + const stack = `Error: foo |
| 132 | + at innerEval (eval at outerEval (eval at topEval (http://www.example.org/script.js:10:5)), <anonymous>:1:1)`; |
| 133 | + const frames = StackTraceImpl.DetailedErrorStackParser.parseRawFramesFromErrorStack(stack); |
| 134 | + |
| 135 | + assert.lengthOf(frames, 1); |
| 136 | + assert.isTrue(frames[0].parsedFrameInfo?.isEval); |
| 137 | + assert.strictEqual(frames[0].url, '<anonymous>'); |
| 138 | + assert.strictEqual(frames[0].lineNumber, 0); |
| 139 | + assert.strictEqual(frames[0].columnNumber, 0); |
| 140 | + |
| 141 | + // Level 1: outerEval |
| 142 | + const outerEvalOrigin = frames[0].parsedFrameInfo?.evalOrigin; |
| 143 | + assert.exists(outerEvalOrigin); |
| 144 | + assert.isTrue(outerEvalOrigin?.parsedFrameInfo?.isEval); |
| 145 | + assert.strictEqual(outerEvalOrigin?.functionName, 'outerEval'); |
| 146 | + assert.strictEqual(outerEvalOrigin?.url, ''); // no <anonymous> suffix |
| 147 | + assert.strictEqual(outerEvalOrigin?.lineNumber, -1); |
| 148 | + assert.strictEqual(outerEvalOrigin?.columnNumber, -1); |
| 149 | + |
| 150 | + // Level 2: topEval |
| 151 | + const topEvalOrigin = outerEvalOrigin?.parsedFrameInfo?.evalOrigin; |
| 152 | + assert.exists(topEvalOrigin); |
| 153 | + assert.isFalse(topEvalOrigin?.parsedFrameInfo?.isEval); |
| 154 | + assert.strictEqual(topEvalOrigin?.functionName, 'topEval'); |
| 155 | + assert.strictEqual(topEvalOrigin?.url, 'http://www.example.org/script.js'); |
| 156 | + assert.strictEqual(topEvalOrigin?.lineNumber, 9); |
| 157 | + assert.strictEqual(topEvalOrigin?.columnNumber, 4); |
| 158 | + }); |
| 159 | + |
| 160 | + it('parses aliased method calls', () => { |
| 161 | + const stack = `Error: foo |
| 162 | + at Type.method [as alias] (http://www.example.org/script.js:10:5)`; |
| 163 | + const frames = StackTraceImpl.DetailedErrorStackParser.parseRawFramesFromErrorStack(stack); |
| 164 | + |
| 165 | + assert.lengthOf(frames, 1); |
| 166 | + assert.strictEqual(frames[0].parsedFrameInfo?.typeName, 'Type'); |
| 167 | + assert.strictEqual(frames[0].parsedFrameInfo?.methodName, 'alias'); |
| 168 | + }); |
| 169 | + |
| 170 | + it('parses wasm frames', () => { |
| 171 | + const stack = `Error: foo |
| 172 | + at wasmModule.wasmFunc (http://www.example.org/script.js:wasm-function[123]:0xabc)`; |
| 173 | + const frames = StackTraceImpl.DetailedErrorStackParser.parseRawFramesFromErrorStack(stack); |
| 174 | + |
| 175 | + assert.lengthOf(frames, 1); |
| 176 | + assert.isTrue(frames[0].parsedFrameInfo?.isWasm); |
| 177 | + assert.strictEqual(frames[0].url, 'http://www.example.org/script.js'); |
| 178 | + assert.strictEqual(frames[0].parsedFrameInfo?.wasmModuleName, 'wasmModule'); |
| 179 | + assert.strictEqual(frames[0].parsedFrameInfo?.wasmFunctionIndex, 123); |
| 180 | + assert.strictEqual(frames[0].columnNumber, 0xabc); |
| 181 | + }); |
| 182 | + |
| 183 | + it('parses promise.all index', () => { |
| 184 | + const stack = `Error: foo |
| 185 | + at Promise.all (index 2)`; |
| 186 | + const frames = StackTraceImpl.DetailedErrorStackParser.parseRawFramesFromErrorStack(stack); |
| 187 | + |
| 188 | + assert.lengthOf(frames, 1); |
| 189 | + assert.strictEqual(frames[0].parsedFrameInfo?.promiseIndex, 2); |
| 190 | + assert.strictEqual(frames[0].url, ''); |
| 191 | + assert.strictEqual(frames[0].functionName, 'Promise.all'); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments