|
| 1 | +import { stripIndent } from 'common-tags'; |
| 2 | +import path from 'path'; |
| 3 | +import { |
| 4 | + formatStackTraceWithInternals, |
| 5 | + MODULE_ROOT, |
| 6 | + stringifyStackTrace, |
| 7 | +} from '../../../src/utils/stack-trace/helpers'; |
| 8 | + |
| 9 | +function generateMockCallSite( |
| 10 | + str = `someTest (randomFakeFile.js:10:10)`, |
| 11 | + fileName = 'randomFakeFile.js' |
| 12 | +) { |
| 13 | + return { |
| 14 | + getThis: jest.fn(), |
| 15 | + getTypeName: jest.fn(), |
| 16 | + getFunction: jest.fn(), |
| 17 | + getFunctionName: jest.fn(), |
| 18 | + getMethodName: jest.fn(), |
| 19 | + getFileName: jest.fn().mockReturnValue(fileName), |
| 20 | + getLineNumber: jest.fn(), |
| 21 | + getColumnNumber: jest.fn(), |
| 22 | + getEvalOrigin: jest.fn(), |
| 23 | + isToplevel: jest.fn(), |
| 24 | + isEval: jest.fn(), |
| 25 | + isNative: jest.fn(), |
| 26 | + isConstructor: jest.fn(), |
| 27 | + isAsync: jest.fn(), |
| 28 | + isPromiseAll: jest.fn(), |
| 29 | + getPromiseIndex: jest.fn(), |
| 30 | + toString: () => { |
| 31 | + return str; |
| 32 | + }, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +describe('MODULE_ROOT', () => { |
| 37 | + test('module root points at root of source code', () => { |
| 38 | + expect(MODULE_ROOT.endsWith('src')).toBe(true); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe('stringifyStackTrace', () => { |
| 43 | + test('formats error correctly', () => { |
| 44 | + const err = new Error('Test error message'); |
| 45 | + err.name = 'FakeTestError'; |
| 46 | + const callsites = [generateMockCallSite()]; |
| 47 | + const stackString = stringifyStackTrace(err, callsites); |
| 48 | + expect(stackString).toEqual(stripIndent` |
| 49 | + FakeTestError: Test error message |
| 50 | + at someTest (randomFakeFile.js:10:10) |
| 51 | + `); |
| 52 | + }); |
| 53 | + |
| 54 | + test('handles multiple callsites', () => { |
| 55 | + const err = new Error('Test error message'); |
| 56 | + err.name = 'FakeTestError'; |
| 57 | + const callsites = [ |
| 58 | + generateMockCallSite(), |
| 59 | + generateMockCallSite('anotherTest (randomFakeFile.js:20:0)'), |
| 60 | + ]; |
| 61 | + const stackString = stringifyStackTrace(err, callsites); |
| 62 | + expect(stackString).toEqual(stripIndent` |
| 63 | + FakeTestError: Test error message |
| 64 | + at someTest (randomFakeFile.js:10:10) |
| 65 | + at anotherTest (randomFakeFile.js:20:0) |
| 66 | + `); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe('formatStackTraceWithInternals', () => { |
| 71 | + test('does not add "internals" footer if no internals', () => { |
| 72 | + const err = new Error('Test error message'); |
| 73 | + err.name = 'FakeTestError'; |
| 74 | + const callsites = [generateMockCallSite()]; |
| 75 | + const stackString = formatStackTraceWithInternals(err, callsites); |
| 76 | + expect(stackString).toEqual(stripIndent` |
| 77 | + FakeTestError: Test error message |
| 78 | + at someTest (randomFakeFile.js:10:10) |
| 79 | + `); |
| 80 | + }); |
| 81 | + |
| 82 | + test('adds "internals" footer if internals are present', () => { |
| 83 | + const err = new Error('Test error message'); |
| 84 | + err.name = 'FakeTestError'; |
| 85 | + const callsites = [ |
| 86 | + generateMockCallSite(), |
| 87 | + generateMockCallSite( |
| 88 | + 'randomInternalThatShouldBeRemoved (randomFakeFile.js:20:0)', |
| 89 | + path.join(MODULE_ROOT, 'randomFakeFile.js') |
| 90 | + ), |
| 91 | + ]; |
| 92 | + const stackString = formatStackTraceWithInternals(err, callsites); |
| 93 | + expect(stackString).toEqual(stripIndent` |
| 94 | + FakeTestError: Test error message |
| 95 | + at someTest (randomFakeFile.js:10:10) |
| 96 | + at [Twilio Dev Server internals] |
| 97 | + `); |
| 98 | + }); |
| 99 | +}); |
0 commit comments