1- import { ErrorWithStack , copyStackTrace } from '../errors' ;
1+ import { copyStackTrace , ErrorWithStack } from '../errors' ;
22
33describe ( 'ErrorWithStack' , ( ) => {
4- test ( 'should create an error with message and handle stack trace capture ' , ( ) => {
4+ test ( 'should create an error with message' , ( ) => {
55 const error = new ErrorWithStack ( 'Test error' , ErrorWithStack ) ;
66 expect ( error . message ) . toBe ( 'Test error' ) ;
77 expect ( error ) . toBeInstanceOf ( Error ) ;
8- } ) ;
98
10- test ( 'should capture stack trace if Error.captureStackTrace is available' , ( ) => {
119 const originalCaptureStackTrace = Error . captureStackTrace ;
12- const captureStackTraceSpy = jest . fn ( ) ;
13- Error . captureStackTrace = captureStackTraceSpy ;
10+ // @ts -expect-error - intentionally removing captureStackTrace
11+ delete Error . captureStackTrace ;
1412
15- const error = new ErrorWithStack ( 'Test error' , ErrorWithStack ) ;
16- expect ( captureStackTraceSpy ) . toHaveBeenCalledWith ( error , ErrorWithStack ) ;
13+ const errorWithoutCapture = new ErrorWithStack ( 'Test error' , ErrorWithStack ) ;
14+ expect ( errorWithoutCapture . message ) . toBe ( 'Test error' ) ;
15+ expect ( errorWithoutCapture ) . toBeInstanceOf ( Error ) ;
1716
1817 Error . captureStackTrace = originalCaptureStackTrace ;
1918 } ) ;
2019
21- test ( 'should work when Error.captureStackTrace is not available' , ( ) => {
20+ test ( 'should capture stack trace if Error.captureStackTrace is available' , ( ) => {
2221 const originalCaptureStackTrace = Error . captureStackTrace ;
23- // @ts -expect-error - intentionally removing captureStackTrace
24- delete Error . captureStackTrace ;
22+ const captureStackTraceSpy = jest . fn ( ) ;
23+ Error . captureStackTrace = captureStackTraceSpy ;
2524
2625 const error = new ErrorWithStack ( 'Test error' , ErrorWithStack ) ;
27- expect ( error . message ) . toBe ( 'Test error' ) ;
28- expect ( error ) . toBeInstanceOf ( Error ) ;
26+ expect ( captureStackTraceSpy ) . toHaveBeenCalledWith ( error , ErrorWithStack ) ;
2927
3028 Error . captureStackTrace = originalCaptureStackTrace ;
3129 } ) ;
@@ -38,20 +36,16 @@ describe('copyStackTrace', () => {
3836 source . stack = 'Error: Source error\n at test.js:1:1' ;
3937
4038 copyStackTrace ( target , source ) ;
41-
4239 expect ( target . stack ) . toBe ( 'Error: Target error\n at test.js:1:1' ) ;
43- } ) ;
4440
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 =
41+ const target2 = new Error ( 'Target error' ) ;
42+ const source2 = new Error ( 'Source error' ) ;
43+ source2 . stack =
4944 'Error: Source error\n at test.js:1:1\nError: Source error\n at test.js:2:2' ;
5045
51- copyStackTrace ( target , source ) ;
52-
46+ copyStackTrace ( target2 , source2 ) ;
5347 // Should replace only the first occurrence
54- expect ( target . stack ) . toBe (
48+ expect ( target2 . stack ) . toBe (
5549 'Error: Target error\n at test.js:1:1\nError: Source error\n at test.js:2:2' ,
5650 ) ;
5751 } ) ;
0 commit comments