node <<'NODE' const assert = require('assert'); const ErrorStackParser = require('error-stack-parser'); const StackFrame = require('stackframe'); const StackGenerator = require('stack-generator'); const StackTrace = require('stacktrace-js'); const StackTraceGPS = require('stacktrace-gps'); function isLegacyStackGeneratorError(error) { return error instanceof TypeError && /'caller', 'callee', and 'arguments' properties may not be accessed/.test(error.message); } function stackFrameObject(stackFrame) { return { functionName: stackFrame.getFunctionName(), fileName: stackFrame.getFileName(), lineNumber: stackFrame.getLineNumber(), columnNumber: stackFrame.getColumnNumber() }; } async function testStackFrame() { const stackFrame = new StackFrame({ functionName: 'funName', args: ['args'], fileName: 'http://localhost:3000/file.js', lineNumber: 1, columnNumber: 3288, isEval: true, isNative: false, source: 'ORIGINAL_STACK_LINE' }); stackFrame.setFunctionName('newName'); assert.strictEqual(stackFrame.getFunctionName(), 'newName'); stackFrame.setArgs([]); assert.deepStrictEqual(stackFrame.getArgs(), []); stackFrame.setFileName('http://localhost:3000/file.js'); assert.strictEqual(stackFrame.getFileName(), 'http://localhost:3000/file.js'); stackFrame.setLineNumber(325); assert.strictEqual(stackFrame.getLineNumber(), 325); stackFrame.setColumnNumber(20); assert.strictEqual(stackFrame.getColumnNumber(), 20); stackFrame.setSource('NEW_SOURCE'); assert.strictEqual(stackFrame.getSource(), 'NEW_SOURCE'); stackFrame.setIsEval(false); assert.strictEqual(stackFrame.getIsEval(), false); stackFrame.setIsNative(true); assert.strictEqual(stackFrame.getIsNative(), true); assert.strictEqual(stackFrame.toString(), 'newName (http://localhost:3000/file.js:325:20)'); const parsed = StackFrame.fromString('fn(arg1,arg2)@http://example.test/file.js:10:20'); assert.strictEqual(parsed.getFunctionName(), 'fn'); assert.deepStrictEqual(parsed.getArgs(), ['arg1', 'arg2']); assert.strictEqual(parsed.getFileName(), 'http://example.test/file.js'); assert.strictEqual(parsed.getLineNumber(), 10); assert.strictEqual(parsed.getColumnNumber(), 20); } async function testErrorStackParser() { const ieError = { message: 'Unable to get property', stack: 'TypeError: Unable to get property\n' + ' at Anonymous function (http://path/to/file.js:47:21)\n' + ' at foo (http://path/to/file.js:45:13)\n' + ' at bar (http://path/to/file.js:108:1)' }; const ieFrames = ErrorStackParser.parse(ieError); assert.strictEqual(ieFrames.length, 3); assert.strictEqual(ieFrames[1].getFunctionName(), 'foo'); assert.strictEqual(ieFrames[1].getFileName(), 'http://path/to/file.js'); assert.strictEqual(ieFrames[1].getLineNumber(), 45); assert.strictEqual(ieFrames[1].getColumnNumber(), 13); const firefoxFrames = ErrorStackParser.parse({ stack: 'alpha@http://example.test/a.js:1:2\nbeta@http://example.test/b.js:3:4' }); assert.strictEqual(firefoxFrames.length, 2); assert.strictEqual(firefoxFrames[0].getFunctionName(), 'alpha'); assert.strictEqual(firefoxFrames[0].getFileName(), 'http://example.test/a.js'); } async function testStackGenerator() { function generatedUnitTest() { return StackGenerator.backtrace({maxStackSize: 3}); } try { const frames = generatedUnitTest(); assert(Array.isArray(frames)); assert(frames.length > 0); assert(frames.some(frame => frame.getFunctionName() === 'generatedUnitTest')); } catch (error) { assert(isLegacyStackGeneratorError(error)); } } async function testStackTraceGPS() { const sourceCache = { 'http://path/to/file.js': 'function increment(){\nsomeVariable+=2;\nnull.x()\n}\nvar someVariable=2;increment();', 'http://path/to/file.min.js': 'function increment(){someVariable+=2;null.x()}var someVariable=2;increment();\n//# sourceMappingURL=file.min.js.map', '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"}' }; const gps = new StackTraceGPS({offline: true, sourceCache}); const mapped = await gps.pinpoint(new StackFrame({ functionName: 'increment', fileName: 'http://path/to/file.min.js', lineNumber: 1, columnNumber: 38 })); assert.strictEqual(mapped.getFunctionName(), 'increment'); assert.strictEqual(mapped.getFileName(), 'http://path/to/file.js'); assert.strictEqual(mapped.getLineNumber(), 3); assert.strictEqual(mapped.getColumnNumber(), 4); } async function testStackTrace() { assert.strictEqual(typeof StackTrace.get, 'function'); assert.strictEqual(typeof StackTrace.getSync, 'function'); assert.strictEqual(typeof StackTrace.fromError, 'function'); const syncFrames = StackTrace.getSync({filter: null}); assert(Array.isArray(syncFrames)); assert(syncFrames.length > 0); try { function artificialFrameTarget() { return StackTrace.generateArtificially({ filter: frame => /artificialFrameTarget/.test(frame.functionName || '') }); } const artificialFrames = await artificialFrameTarget(); assert(Array.isArray(artificialFrames)); assert(artificialFrames.length > 0); } catch (error) { assert(isLegacyStackGeneratorError(error)); } await assert.rejects(StackTrace.fromError({message: 'ERROR_MESSAGE'})); const errorFrames = await StackTrace.fromError({ stack: 'TypeError: Unable to get property\n' + ' at Anonymous function (http://path/to/file.js:47:21)\n' + ' at foo (http://path/to/file.js:45:13)\n' + ' at bar (http://path/to/file.js:108:1)' }, { offline: true, sourceCache: {'http://path/to/file.js': 'function foo() {}\nfunction bar() {}'} }); assert.strictEqual(errorFrames.length, 3); assert.throws(() => StackTrace.instrument('BOGUS'), /Cannot instrument non-function object/); assert.throws(() => StackTrace.deinstrument('BOGUS'), /Cannot de-instrument non-function object/); function interestingFn() { return 'something'; } const wrapped = StackTrace.instrument(interestingFn, frames => { assert(Array.isArray(frames)); }); assert.strictEqual(wrapped(), 'something'); assert.strictEqual(StackTrace.deinstrument(wrapped), interestingFn); } async function testReport() { const requests = []; global.XMLHttpRequest = function XMLHttpRequestMock() { this.readyState = 0; this.status = 0; this.requestHeaders = {}; this.open = (method, url) => { this.method = method; this.url = url; }; this.setRequestHeader = (key, value) => { this.requestHeaders[key] = value; }; this.send = body => { this.body = body; requests.push(this); }; this.respond = (status, responseText) => { this.status = status; this.responseText = responseText; this.readyState = 4; this.onreadystatechange(); }; }; const stackframes = [new StackFrame({functionName: 'fn', fileName: 'file.js', lineNumber: 32, columnNumber: 1})]; const reportPromise = StackTrace.report(stackframes, 'http://domain.ext/endpoint', 'BOOM', { headers: {'Access-Control-Request-Method': 'POST'} }); assert.strictEqual(requests.length, 1); assert.strictEqual(requests[0].method, 'post'); assert.strictEqual(requests[0].url, 'http://domain.ext/endpoint'); assert.deepStrictEqual(requests[0].requestHeaders, { 'Content-Type': 'application/json', 'Access-Control-Request-Method': 'POST' }); assert.deepStrictEqual(JSON.parse(requests[0].body), { message: 'BOOM', stack: stackframes.map(stackFrameObject) }); requests[0].respond(201, 'OK'); assert.strictEqual(await reportPromise, 'OK'); const failedReport = StackTrace.report(stackframes, 'http://domain.ext/fail'); requests[1].respond(500, 'nope'); await assert.rejects(failedReport, /POST to http:\/\/domain.ext\/fail failed/); delete global.XMLHttpRequest; } (async () => { await testStackFrame(); await testErrorStackParser(); await testStackGenerator(); await testStackTraceGPS(); await testStackTrace(); await testReport(); })(); NODE tsc --noEmit --target es5 node_modules/stackframe/stackframe-tests.ts