|
| 1 | +import { expect } from 'earl' |
| 2 | +import Parser from 'error-stack-parser' |
| 3 | +import { utils } from 'mocha' |
| 4 | + |
| 5 | +function captureError(fn) { |
| 6 | + try { |
| 7 | + fn() |
| 8 | + } catch (error) { |
| 9 | + if (error instanceof Error) { |
| 10 | + return error |
| 11 | + } |
| 12 | + throw error |
| 13 | + } |
| 14 | + throw new Error('No error captured!') |
| 15 | +} |
| 16 | + |
| 17 | +async function captureErrorAsync(fn) { |
| 18 | + try { |
| 19 | + await fn() |
| 20 | + } catch (error) { |
| 21 | + if (error instanceof Error) { |
| 22 | + return error |
| 23 | + } |
| 24 | + throw error |
| 25 | + } |
| 26 | + throw new Error('No error captured!') |
| 27 | +} |
| 28 | + |
| 29 | +function getStack(error) { |
| 30 | + const stack = utils.stackTraceFilter()(error.stack ?? '') |
| 31 | + return Parser.parse({ stack }).map((x) => ({ |
| 32 | + at: x.functionName, |
| 33 | + file: x.fileName, |
| 34 | + })) |
| 35 | +} |
| 36 | + |
| 37 | +describe('stack traces', () => { |
| 38 | + it('captures correct synchronous stack traces', () => { |
| 39 | + const error = captureError(() => expect(1).toEqual(2)) |
| 40 | + const stack = getStack(error) |
| 41 | + |
| 42 | + expect(stack[0]).toEqual({ |
| 43 | + at: 'expect().toEqual', |
| 44 | + file: expect.includes('stack-traces.test.mjs'), |
| 45 | + }) |
| 46 | + expect(stack[1]).toEqual({ |
| 47 | + at: expect.a(String), |
| 48 | + file: expect.includes('stack-traces.test.mjs'), |
| 49 | + }) |
| 50 | + expect(stack[2]).toEqual({ |
| 51 | + at: expect.a(String), |
| 52 | + file: expect.includes('stack-traces.test.mjs'), |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + it('captures correct negated synchronous stack traces', () => { |
| 57 | + const error = captureError(() => expect(1).not.toEqual(1)) |
| 58 | + const stack = getStack(error) |
| 59 | + |
| 60 | + expect(stack[0]).toEqual({ |
| 61 | + at: 'expect().not.toEqual', |
| 62 | + file: expect.includes('stack-traces.test.mjs'), |
| 63 | + }) |
| 64 | + expect(stack[1]).toEqual({ |
| 65 | + at: expect.a(String), |
| 66 | + file: expect.includes('stack-traces.test.mjs'), |
| 67 | + }) |
| 68 | + expect(stack[2]).toEqual({ |
| 69 | + at: expect.a(String), |
| 70 | + file: expect.includes('stack-traces.test.mjs'), |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + it('captures correct synchronous stack traces for toThrow', () => { |
| 75 | + const error = captureError(() => |
| 76 | + expect(() => { |
| 77 | + throw new Error('foo') |
| 78 | + }).toThrow('bar'), |
| 79 | + ) |
| 80 | + const stack = getStack(error) |
| 81 | + |
| 82 | + expect(stack[0]).toEqual({ |
| 83 | + at: 'expect().toThrow', |
| 84 | + file: expect.includes('stack-traces.test.mjs'), |
| 85 | + }) |
| 86 | + expect(stack[1]).toEqual({ |
| 87 | + at: expect.a(String), |
| 88 | + file: expect.includes('stack-traces.test.mjs'), |
| 89 | + }) |
| 90 | + expect(stack[2]).toEqual({ |
| 91 | + at: expect.a(String), |
| 92 | + file: expect.includes('stack-traces.test.mjs'), |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + it('captures correct asynchronous stack traces', async () => { |
| 97 | + const error = await captureErrorAsync(async () => |
| 98 | + expect(await Promise.resolve(1)).toEqual(2), |
| 99 | + ) |
| 100 | + const stack = getStack(error) |
| 101 | + |
| 102 | + expect(stack[0]).toEqual({ |
| 103 | + at: 'expect().toEqual', |
| 104 | + file: expect.includes('stack-traces.test.mjs'), |
| 105 | + }) |
| 106 | + expect(stack[1]).toEqual({ |
| 107 | + at: expect.a(String), |
| 108 | + file: expect.includes('stack-traces.test.mjs'), |
| 109 | + }) |
| 110 | + expect(stack[2]).toEqual({ |
| 111 | + at: expect.a(String), |
| 112 | + file: expect.includes('stack-traces.test.mjs'), |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + it('captures correct asynchronous stack traces for toBeRejectedWith', async () => { |
| 117 | + const error = await captureErrorAsync(() => |
| 118 | + expect(async () => { |
| 119 | + throw new Error('foo') |
| 120 | + }).toBeRejectedWith('bar'), |
| 121 | + ) |
| 122 | + const stack = getStack(error) |
| 123 | + |
| 124 | + expect(stack[0]).toEqual({ |
| 125 | + at: 'expect().toBeRejectedWith', |
| 126 | + file: expect.includes('stack-traces.test.mjs'), |
| 127 | + }) |
| 128 | + expect(stack[1]).toEqual({ |
| 129 | + at: expect.a(String), |
| 130 | + file: expect.includes('stack-traces.test.mjs'), |
| 131 | + }) |
| 132 | + expect(stack[2]).toEqual({ |
| 133 | + at: expect.a(String), |
| 134 | + file: expect.includes('stack-traces.test.mjs'), |
| 135 | + }) |
| 136 | + }) |
| 137 | +}) |
0 commit comments