|
| 1 | +import { copyStackTrace, ErrorWithStack } from '../errors'; |
| 2 | + |
| 3 | +describe('ErrorWithStack', () => { |
| 4 | + test('should create an error with message', () => { |
| 5 | + const error = new ErrorWithStack('Test error', ErrorWithStack); |
| 6 | + expect(error.message).toBe('Test error'); |
| 7 | + expect(error).toBeInstanceOf(Error); |
| 8 | + |
| 9 | + const originalCaptureStackTrace = Error.captureStackTrace; |
| 10 | + // @ts-expect-error - intentionally removing captureStackTrace |
| 11 | + delete Error.captureStackTrace; |
| 12 | + |
| 13 | + const errorWithoutCapture = new ErrorWithStack('Test error', ErrorWithStack); |
| 14 | + expect(errorWithoutCapture.message).toBe('Test error'); |
| 15 | + expect(errorWithoutCapture).toBeInstanceOf(Error); |
| 16 | + |
| 17 | + Error.captureStackTrace = originalCaptureStackTrace; |
| 18 | + }); |
| 19 | + |
| 20 | + test('should capture stack trace if Error.captureStackTrace is available', () => { |
| 21 | + const originalCaptureStackTrace = Error.captureStackTrace; |
| 22 | + const captureStackTraceSpy = jest.fn(); |
| 23 | + Error.captureStackTrace = captureStackTraceSpy; |
| 24 | + |
| 25 | + const error = new ErrorWithStack('Test error', ErrorWithStack); |
| 26 | + expect(captureStackTraceSpy).toHaveBeenCalledWith(error, ErrorWithStack); |
| 27 | + |
| 28 | + Error.captureStackTrace = originalCaptureStackTrace; |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe('copyStackTrace', () => { |
| 33 | + test('should copy stack trace from source to target when both are Error instances', () => { |
| 34 | + const target = new Error('Target error'); |
| 35 | + const source = new Error('Source error'); |
| 36 | + source.stack = 'Error: Source error\n at test.js:1:1'; |
| 37 | + |
| 38 | + copyStackTrace(target, source); |
| 39 | + expect(target.stack).toBe('Error: Target error\n at test.js:1:1'); |
| 40 | + |
| 41 | + const target2 = new Error('Target error'); |
| 42 | + const source2 = new Error('Source error'); |
| 43 | + source2.stack = |
| 44 | + 'Error: Source error\n at test.js:1:1\nError: Source error\n at test.js:2:2'; |
| 45 | + |
| 46 | + copyStackTrace(target2, source2); |
| 47 | + // Should replace only the first occurrence |
| 48 | + expect(target2.stack).toBe( |
| 49 | + 'Error: Target error\n at test.js:1:1\nError: Source error\n at test.js:2:2', |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should not modify target when conditions are not met', () => { |
| 54 | + const targetNotError = { message: 'Not an error' }; |
| 55 | + const source = new Error('Source error'); |
| 56 | + source.stack = 'Error: Source error\n at test.js:1:1'; |
| 57 | + |
| 58 | + copyStackTrace(targetNotError, source); |
| 59 | + expect(targetNotError).toEqual({ message: 'Not an error' }); |
| 60 | + |
| 61 | + const target = new Error('Target error'); |
| 62 | + const originalStack = target.stack; |
| 63 | + const sourceNotError = { message: 'Not an error' }; |
| 64 | + |
| 65 | + copyStackTrace(target, sourceNotError as Error); |
| 66 | + expect(target.stack).toBe(originalStack); |
| 67 | + |
| 68 | + const sourceNoStack = new Error('Source error'); |
| 69 | + delete sourceNoStack.stack; |
| 70 | + |
| 71 | + copyStackTrace(target, sourceNoStack); |
| 72 | + expect(target.stack).toBe(originalStack); |
| 73 | + }); |
| 74 | +}); |
0 commit comments