|
| 1 | +/** |
| 2 | + * stack-trace - Parses node.js stack traces |
| 3 | + * |
| 4 | + * These tests were originally forked to fix this issue: |
| 5 | + * https://github.com/felixge/node-stack-trace/issues/31 |
| 6 | + * |
| 7 | + * Mar 19,2019 - #4fd379e |
| 8 | + * |
| 9 | + * https://github.com/felixge/node-stack-trace/ |
| 10 | + * @license MIT |
| 11 | + */ |
| 12 | + |
| 13 | +import * as stacktrace from '../src/stack-trace'; |
| 14 | + |
| 15 | +// tslint:disable:typedef |
| 16 | +// tslint:disable:prefer-template |
| 17 | + |
| 18 | +function testBasic() { |
| 19 | + return new Error('something went wrong'); |
| 20 | +} |
| 21 | + |
| 22 | +function testWrapper() { |
| 23 | + return testBasic(); |
| 24 | +} |
| 25 | + |
| 26 | +describe('stack-trace.ts', () => { |
| 27 | + test('testObjectInMethodName', () => { |
| 28 | + const err: { [key: string]: any } = {}; |
| 29 | + err.stack = |
| 30 | + 'Error: Foo\n' + |
| 31 | + ' at [object Object].global.every [as _onTimeout] (/Users/hoitz/develop/test.coffee:36:3)\n' + |
| 32 | + ' at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)\n'; |
| 33 | + |
| 34 | + const trace = stacktrace.parse(err as Error); |
| 35 | + |
| 36 | + expect(trace[0].fileName).toEqual('/Users/hoitz/develop/test.coffee'); |
| 37 | + expect(trace[1].fileName).toEqual('timers.js'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('testBasic', () => { |
| 41 | + const trace = stacktrace.parse(testBasic()); |
| 42 | + |
| 43 | + expect(trace[0].fileName).toEqual(__filename); |
| 44 | + expect(trace[0].functionName).toEqual('testBasic'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('testWrapper', () => { |
| 48 | + const trace = stacktrace.parse(testWrapper()); |
| 49 | + |
| 50 | + expect(trace[0].functionName).toEqual('testBasic'); |
| 51 | + expect(trace[1].functionName).toEqual('testWrapper'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('testNoStack', () => { |
| 55 | + const err = { stack: undefined }; |
| 56 | + const trace = stacktrace.parse(err as Error); |
| 57 | + |
| 58 | + expect(trace).toEqual([]); |
| 59 | + }); |
| 60 | + |
| 61 | + test('testCorruptStack', () => { |
| 62 | + const err: { [key: string]: any } = {}; |
| 63 | + err.stack = |
| 64 | + 'AssertionError: true == false\n' + |
| 65 | + ' fuck' + |
| 66 | + ' at Test.run (/Users/felix/code/node-fast-or-slow/lib/test.js:45:10)\n' + |
| 67 | + 'oh no' + |
| 68 | + ' at TestCase.run (/Users/felix/code/node-fast-or-slow/lib/test_case.js:61:8)\n'; |
| 69 | + |
| 70 | + const trace = stacktrace.parse(err as Error); |
| 71 | + |
| 72 | + expect(trace.length).toEqual(2); |
| 73 | + }); |
| 74 | + |
| 75 | + test('testTraceWitoutColumnNumbers', () => { |
| 76 | + const err: { [key: string]: any } = {}; |
| 77 | + err.stack = |
| 78 | + 'AssertionError: true == false\n' + |
| 79 | + ' at Test.fn (/Users/felix/code/node-fast-or-slow/test/fast/example/test-example.js:6)\n' + |
| 80 | + ' at Test.run (/Users/felix/code/node-fast-or-slow/lib/test.js:45)'; |
| 81 | + |
| 82 | + const trace = stacktrace.parse(err as Error); |
| 83 | + |
| 84 | + expect(trace[0].fileName).toEqual('/Users/felix/code/node-fast-or-slow/test/fast/example/test-example.js'); |
| 85 | + expect(trace[0].lineNumber).toEqual(6); |
| 86 | + expect(trace[0].columnNumber).toEqual(null); |
| 87 | + }); |
| 88 | + |
| 89 | + test('testStackWithNativeCall', () => { |
| 90 | + const err: { [key: string]: any } = {}; |
| 91 | + err.stack = |
| 92 | + 'AssertionError: true == false\n' + |
| 93 | + ' at Test.fn (/Users/felix/code/node-fast-or-slow/test/fast/example/test-example.js:6:10)\n' + |
| 94 | + ' at Test.run (/Users/felix/code/node-fast-or-slow/lib/test.js:45:10)\n' + |
| 95 | + ' at TestCase.runNext (/Users/felix/code/node-fast-or-slow/lib/test_case.js:73:8)\n' + |
| 96 | + ' at TestCase.run (/Users/felix/code/node-fast-or-slow/lib/test_case.js:61:8)\n' + |
| 97 | + ' at Array.0 (native)\n' + |
| 98 | + ' at EventEmitter._tickCallback (node.js:126:26)'; |
| 99 | + |
| 100 | + const trace = stacktrace.parse(err as Error); |
| 101 | + const nativeCallSite = trace[4]; |
| 102 | + |
| 103 | + expect(nativeCallSite.fileName).toEqual(null); |
| 104 | + expect(nativeCallSite.functionName).toEqual('Array.0'); |
| 105 | + expect(nativeCallSite.typeName).toEqual('Array'); |
| 106 | + expect(nativeCallSite.methodName).toEqual('0'); |
| 107 | + expect(nativeCallSite.lineNumber).toEqual(null); |
| 108 | + expect(nativeCallSite.columnNumber).toEqual(null); |
| 109 | + expect(nativeCallSite.native).toEqual(true); |
| 110 | + }); |
| 111 | + |
| 112 | + test('testStackWithFileOnly', () => { |
| 113 | + const err: { [key: string]: any } = {}; |
| 114 | + err.stack = 'AssertionError: true == false\n' + ' at /Users/felix/code/node-fast-or-slow/lib/test_case.js:80:10'; |
| 115 | + |
| 116 | + const trace = stacktrace.parse(err as Error); |
| 117 | + const callSite = trace[0]; |
| 118 | + |
| 119 | + expect(callSite.fileName).toEqual('/Users/felix/code/node-fast-or-slow/lib/test_case.js'); |
| 120 | + expect(callSite.functionName).toEqual(null); |
| 121 | + expect(callSite.typeName).toEqual(null); |
| 122 | + expect(callSite.methodName).toEqual(null); |
| 123 | + expect(callSite.lineNumber).toEqual(80); |
| 124 | + expect(callSite.columnNumber).toEqual(10); |
| 125 | + expect(callSite.native).toEqual(false); |
| 126 | + }); |
| 127 | + |
| 128 | + test('testStackWithMultilineMessage', () => { |
| 129 | + const err: { [key: string]: any } = {}; |
| 130 | + err.stack = |
| 131 | + 'AssertionError: true == false\nAnd some more shit\n' + |
| 132 | + ' at /Users/felix/code/node-fast-or-slow/lib/test_case.js:80:10'; |
| 133 | + |
| 134 | + const trace = stacktrace.parse(err as Error); |
| 135 | + const callSite = trace[0]; |
| 136 | + |
| 137 | + expect(callSite.fileName).toEqual('/Users/felix/code/node-fast-or-slow/lib/test_case.js'); |
| 138 | + }); |
| 139 | + |
| 140 | + test('testStackWithAnonymousFunctionCall', () => { |
| 141 | + const err: { [key: string]: any } = {}; |
| 142 | + err.stack = |
| 143 | + 'AssertionError: expected [] to be arguments\n' + |
| 144 | + ' at Assertion.prop.(anonymous function) (/Users/den/Projects/should.js/lib/should.js:60:14)\n'; |
| 145 | + |
| 146 | + const trace = stacktrace.parse(err as Error); |
| 147 | + const callSite0 = trace[0]; |
| 148 | + |
| 149 | + expect(callSite0.fileName).toEqual('/Users/den/Projects/should.js/lib/should.js'); |
| 150 | + expect(callSite0.functionName).toEqual('Assertion.prop.(anonymous function)'); |
| 151 | + expect(callSite0.typeName).toEqual('Assertion.prop'); |
| 152 | + expect(callSite0.methodName).toEqual('(anonymous function)'); |
| 153 | + expect(callSite0.lineNumber).toEqual(60); |
| 154 | + expect(callSite0.columnNumber).toEqual(14); |
| 155 | + expect(callSite0.native).toEqual(false); |
| 156 | + }); |
| 157 | + |
| 158 | + test('testTraceBracesInPath', () => { |
| 159 | + const err: { [key: string]: any } = {}; |
| 160 | + err.stack = |
| 161 | + 'AssertionError: true == false\n' + |
| 162 | + ' at Test.run (/Users/felix (something)/code/node-fast-or-slow/lib/test.js:45:10)\n' + |
| 163 | + ' at TestCase.run (/Users/felix (something)/code/node-fast-or-slow/lib/test_case.js:61:8)\n'; |
| 164 | + |
| 165 | + const trace = stacktrace.parse(err as Error); |
| 166 | + |
| 167 | + expect(trace.length).toEqual(2); |
| 168 | + expect(trace[0].fileName).toEqual('/Users/felix (something)/code/node-fast-or-slow/lib/test.js'); |
| 169 | + }); |
| 170 | +}); |
0 commit comments