| 1 |
node <<'NODE' |
| 2 |
const assert = require('assert'); |
| 3 |
const ErrorStackParser = require('error-stack-parser'); |
| 4 |
const StackFrame = require('stackframe'); |
| 5 |
const StackGenerator = require('stack-generator'); |
| 6 |
const StackTrace = require('stacktrace-js'); |
| 7 |
const StackTraceGPS = require('stacktrace-gps'); |
| 8 |
|
| 9 |
function isLegacyStackGeneratorError(error) { |
| 10 |
return error instanceof TypeError && |
| 11 |
/'caller', 'callee', and 'arguments' properties may not be accessed/.test(error.message); |
| 12 |
} |
| 13 |
|
| 14 |
function stackFrameObject(stackFrame) { |
| 15 |
return { |
| 16 |
functionName: stackFrame.getFunctionName(), |
| 17 |
fileName: stackFrame.getFileName(), |
| 18 |
lineNumber: stackFrame.getLineNumber(), |
| 19 |
columnNumber: stackFrame.getColumnNumber() |
| 20 |
}; |
| 21 |
} |
| 22 |
|
| 23 |
async function testStackFrame() { |
| 24 |
const stackFrame = new StackFrame({ |
| 25 |
functionName: 'funName', |
| 26 |
args: ['args'], |
| 27 |
fileName: 'http://localhost:3000/file.js', |
| 28 |
lineNumber: 1, |
| 29 |
columnNumber: 3288, |
| 30 |
isEval: true, |
| 31 |
isNative: false, |
| 32 |
source: 'ORIGINAL_STACK_LINE' |
| 33 |
}); |
| 34 |
|
| 35 |
stackFrame.setFunctionName('newName'); |
| 36 |
assert.strictEqual(stackFrame.getFunctionName(), 'newName'); |
| 37 |
stackFrame.setArgs([]); |
| 38 |
assert.deepStrictEqual(stackFrame.getArgs(), []); |
| 39 |
stackFrame.setFileName('http://localhost:3000/file.js'); |
| 40 |
assert.strictEqual(stackFrame.getFileName(), 'http://localhost:3000/file.js'); |
| 41 |
stackFrame.setLineNumber(325); |
| 42 |
assert.strictEqual(stackFrame.getLineNumber(), 325); |
| 43 |
stackFrame.setColumnNumber(20); |
| 44 |
assert.strictEqual(stackFrame.getColumnNumber(), 20); |
| 45 |
stackFrame.setSource('NEW_SOURCE'); |
| 46 |
assert.strictEqual(stackFrame.getSource(), 'NEW_SOURCE'); |
| 47 |
stackFrame.setIsEval(false); |
| 48 |
assert.strictEqual(stackFrame.getIsEval(), false); |
| 49 |
stackFrame.setIsNative(true); |
| 50 |
assert.strictEqual(stackFrame.getIsNative(), true); |
| 51 |
assert.strictEqual(stackFrame.toString(), 'newName (http://localhost:3000/file.js:325:20)'); |
| 52 |
|
| 53 |
const parsed = StackFrame.fromString('fn(arg1,arg2)@http://example.test/file.js:10:20'); |
| 54 |
assert.strictEqual(parsed.getFunctionName(), 'fn'); |
| 55 |
assert.deepStrictEqual(parsed.getArgs(), ['arg1', 'arg2']); |
| 56 |
assert.strictEqual(parsed.getFileName(), 'http://example.test/file.js'); |
| 57 |
assert.strictEqual(parsed.getLineNumber(), 10); |
| 58 |
assert.strictEqual(parsed.getColumnNumber(), 20); |
| 59 |
} |
| 60 |
|
| 61 |
async function testErrorStackParser() { |
| 62 |
const ieError = { |
| 63 |
message: 'Unable to get property', |
| 64 |
stack: 'TypeError: Unable to get property\n' + |
| 65 |
' at Anonymous function (http://path/to/file.js:47:21)\n' + |
| 66 |
' at foo (http://path/to/file.js:45:13)\n' + |
| 67 |
' at bar (http://path/to/file.js:108:1)' |
| 68 |
}; |
| 69 |
const ieFrames = ErrorStackParser.parse(ieError); |
| 70 |
assert.strictEqual(ieFrames.length, 3); |
| 71 |
assert.strictEqual(ieFrames[1].getFunctionName(), 'foo'); |
| 72 |
assert.strictEqual(ieFrames[1].getFileName(), 'http://path/to/file.js'); |
| 73 |
assert.strictEqual(ieFrames[1].getLineNumber(), 45); |
| 74 |
assert.strictEqual(ieFrames[1].getColumnNumber(), 13); |
| 75 |
|
| 76 |
const firefoxFrames = ErrorStackParser.parse({ |
| 77 |
stack: 'alpha@http://example.test/a.js:1:2\nbeta@http://example.test/b.js:3:4' |
| 78 |
}); |
| 79 |
assert.strictEqual(firefoxFrames.length, 2); |
| 80 |
assert.strictEqual(firefoxFrames[0].getFunctionName(), 'alpha'); |
| 81 |
assert.strictEqual(firefoxFrames[0].getFileName(), 'http://example.test/a.js'); |
| 82 |
} |
| 83 |
|
| 84 |
async function testStackGenerator() { |
| 85 |
function generatedUnitTest() { |
| 86 |
return StackGenerator.backtrace({maxStackSize: 3}); |
| 87 |
} |
| 88 |
|
| 89 |
try { |
| 90 |
const frames = generatedUnitTest(); |
| 91 |
assert(Array.isArray(frames)); |
| 92 |
assert(frames.length > 0); |
| 93 |
assert(frames.some(frame => frame.getFunctionName() === 'generatedUnitTest')); |
| 94 |
} catch (error) { |
| 95 |
assert(isLegacyStackGeneratorError(error)); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
async function testStackTraceGPS() { |
| 100 |
const sourceCache = { |
| 101 |
'http://path/to/file.js': 'function increment(){\nsomeVariable+=2;\nnull.x()\n}\nvar someVariable=2;increment();', |
| 102 |
'http://path/to/file.min.js': 'function increment(){someVariable+=2;null.x()}var someVariable=2;increment();\n//# sourceMappingURL=file.min.js.map', |
| 103 |
'http://path/to/file.min.js.map': '{"version":3,"file":"file.min.js","sources":["file.js"],"names":["increment","someVariable","x"],"mappings":"AAAA,QAASA,aACLC,cAAgB,CAChB,MAAKC,IAET,GAAID,cAAe,CACnBD"}' |
| 104 |
}; |
| 105 |
const gps = new StackTraceGPS({offline: true, sourceCache}); |
| 106 |
const mapped = await gps.pinpoint(new StackFrame({ |
| 107 |
functionName: 'increment', |
| 108 |
fileName: 'http://path/to/file.min.js', |
| 109 |
lineNumber: 1, |
| 110 |
columnNumber: 38 |
| 111 |
})); |
| 112 |
|
| 113 |
assert.strictEqual(mapped.getFunctionName(), 'increment'); |
| 114 |
assert.strictEqual(mapped.getFileName(), 'http://path/to/file.js'); |
| 115 |
assert.strictEqual(mapped.getLineNumber(), 3); |
| 116 |
assert.strictEqual(mapped.getColumnNumber(), 4); |
| 117 |
} |
| 118 |
|
| 119 |
async function testStackTrace() { |
| 120 |
assert.strictEqual(typeof StackTrace.get, 'function'); |
| 121 |
assert.strictEqual(typeof StackTrace.getSync, 'function'); |
| 122 |
assert.strictEqual(typeof StackTrace.fromError, 'function'); |
| 123 |
|
| 124 |
const syncFrames = StackTrace.getSync({filter: null}); |
| 125 |
assert(Array.isArray(syncFrames)); |
| 126 |
assert(syncFrames.length > 0); |
| 127 |
|
| 128 |
try { |
| 129 |
function artificialFrameTarget() { |
| 130 |
return StackTrace.generateArtificially({ |
| 131 |
filter: frame => /artificialFrameTarget/.test(frame.functionName || '') |
| 132 |
}); |
| 133 |
} |
| 134 |
|
| 135 |
const artificialFrames = await artificialFrameTarget(); |
| 136 |
assert(Array.isArray(artificialFrames)); |
| 137 |
assert(artificialFrames.length > 0); |
| 138 |
} catch (error) { |
| 139 |
assert(isLegacyStackGeneratorError(error)); |
| 140 |
} |
| 141 |
|
| 142 |
await assert.rejects(StackTrace.fromError({message: 'ERROR_MESSAGE'})); |
| 143 |
|
| 144 |
const errorFrames = await StackTrace.fromError({ |
| 145 |
stack: 'TypeError: Unable to get property\n' + |
| 146 |
' at Anonymous function (http://path/to/file.js:47:21)\n' + |
| 147 |
' at foo (http://path/to/file.js:45:13)\n' + |
| 148 |
' at bar (http://path/to/file.js:108:1)' |
| 149 |
}, { |
| 150 |
offline: true, |
| 151 |
sourceCache: {'http://path/to/file.js': 'function foo() {}\nfunction bar() {}'} |
| 152 |
}); |
| 153 |
assert.strictEqual(errorFrames.length, 3); |
| 154 |
|
| 155 |
assert.throws(() => StackTrace.instrument('BOGUS'), /Cannot instrument non-function object/); |
| 156 |
assert.throws(() => StackTrace.deinstrument('BOGUS'), /Cannot de-instrument non-function object/); |
| 157 |
|
| 158 |
function interestingFn() { |
| 159 |
return 'something'; |
| 160 |
} |
| 161 |
const wrapped = StackTrace.instrument(interestingFn, frames => { |
| 162 |
assert(Array.isArray(frames)); |
| 163 |
}); |
| 164 |
assert.strictEqual(wrapped(), 'something'); |
| 165 |
assert.strictEqual(StackTrace.deinstrument(wrapped), interestingFn); |
| 166 |
} |
| 167 |
|
| 168 |
async function testReport() { |
| 169 |
const requests = []; |
| 170 |
global.XMLHttpRequest = function XMLHttpRequestMock() { |
| 171 |
this.readyState = 0; |
| 172 |
this.status = 0; |
| 173 |
this.requestHeaders = {}; |
| 174 |
this.open = (method, url) => { |
| 175 |
this.method = method; |
| 176 |
this.url = url; |
| 177 |
}; |
| 178 |
this.setRequestHeader = (key, value) => { |
| 179 |
this.requestHeaders[key] = value; |
| 180 |
}; |
| 181 |
this.send = body => { |
| 182 |
this.body = body; |
| 183 |
requests.push(this); |
| 184 |
}; |
| 185 |
this.respond = (status, responseText) => { |
| 186 |
this.status = status; |
| 187 |
this.responseText = responseText; |
| 188 |
this.readyState = 4; |
| 189 |
this.onreadystatechange(); |
| 190 |
}; |
| 191 |
}; |
| 192 |
|
| 193 |
const stackframes = [new StackFrame({functionName: 'fn', fileName: 'file.js', lineNumber: 32, columnNumber: 1})]; |
| 194 |
const reportPromise = StackTrace.report(stackframes, 'http://domain.ext/endpoint', 'BOOM', { |
| 195 |
headers: {'Access-Control-Request-Method': 'POST'} |
| 196 |
}); |
| 197 |
assert.strictEqual(requests.length, 1); |
| 198 |
assert.strictEqual(requests[0].method, 'post'); |
| 199 |
assert.strictEqual(requests[0].url, 'http://domain.ext/endpoint'); |
| 200 |
assert.deepStrictEqual(requests[0].requestHeaders, { |
| 201 |
'Content-Type': 'application/json', |
| 202 |
'Access-Control-Request-Method': 'POST' |
| 203 |
}); |
| 204 |
assert.deepStrictEqual(JSON.parse(requests[0].body), { |
| 205 |
message: 'BOOM', |
| 206 |
stack: stackframes.map(stackFrameObject) |
| 207 |
}); |
| 208 |
requests[0].respond(201, 'OK'); |
| 209 |
assert.strictEqual(await reportPromise, 'OK'); |
| 210 |
|
| 211 |
const failedReport = StackTrace.report(stackframes, 'http://domain.ext/fail'); |
| 212 |
requests[1].respond(500, 'nope'); |
| 213 |
await assert.rejects(failedReport, /POST to http:\/\/domain.ext\/fail failed/); |
| 214 |
delete global.XMLHttpRequest; |
| 215 |
} |
| 216 |
|
| 217 |
(async () => { |
| 218 |
await testStackFrame(); |
| 219 |
await testErrorStackParser(); |
| 220 |
await testStackGenerator(); |
| 221 |
await testStackTraceGPS(); |
| 222 |
await testStackTrace(); |
| 223 |
await testReport(); |
| 224 |
})(); |
| 225 |
NODE |
| 226 |
|
| 227 |
tsc --noEmit --target es5 node_modules/stackframe/stackframe-tests.ts |