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