@@ -689,6 +689,59 @@ describe('clone()', () => {
689689 expect ( b ) . to . not . shallow . equal ( a ) ;
690690 } ) ;
691691
692+ it ( 'clones Error' , ( ) => {
693+
694+ class CustomError extends Error {
695+ name = 'CustomError' ;
696+ }
697+
698+ const a = new CustomError ( 'bad' ) ;
699+ a . test = Symbol ( 'test' ) ;
700+
701+ const b = Hoek . clone ( a ) ;
702+
703+ expect ( b ) . to . equal ( a ) ;
704+ expect ( b ) . to . not . shallow . equal ( a ) ;
705+ expect ( b ) . to . be . instanceOf ( CustomError ) ;
706+ expect ( b . stack ) . to . equal ( a . stack ) ; // Explicitly validate the .stack getters
707+ } ) ;
708+
709+ it ( 'clones Error with cause' , { skip : process . version . startsWith ( 'v14' ) } , ( ) => {
710+
711+ const a = new TypeError ( 'bad' , { cause : new Error ( 'embedded' ) } ) ;
712+ const b = Hoek . clone ( a ) ;
713+
714+ expect ( b ) . to . equal ( a ) ;
715+ expect ( b ) . to . not . shallow . equal ( a ) ;
716+ expect ( b ) . to . be . instanceOf ( TypeError ) ;
717+ expect ( b . stack ) . to . equal ( a . stack ) ; // Explicitly validate the .stack getters
718+ expect ( b . cause . stack ) . to . equal ( a . cause . stack ) ; // Explicitly validate the .stack getters
719+ } ) ;
720+
721+ it ( 'clones Error with error message' , ( ) => {
722+
723+ const a = new Error ( ) ;
724+ a . message = new Error ( 'message' ) ;
725+
726+ const b = Hoek . clone ( a ) ;
727+
728+ //expect(b).to.equal(a); // deepEqual() always compares message using ===
729+ expect ( b . message ) . to . equal ( a . message ) ;
730+ expect ( b . message ) . to . not . shallow . equal ( a . message ) ;
731+ expect ( b . stack ) . to . equal ( a . stack ) ;
732+ } ) ;
733+
734+ it ( 'cloned Error handles late stack update' , ( ) => {
735+
736+ const a = new Error ( 'bad' ) ;
737+ const b = Hoek . clone ( a ) ;
738+
739+ a . stack = 'late update' ;
740+
741+ expect ( b ) . to . equal ( a ) ;
742+ expect ( b . stack ) . to . not . equal ( a . stack ) ;
743+ } ) ;
744+
692745 it ( 'ignores symbols' , ( ) => {
693746
694747 const sym = Symbol ( ) ;
0 commit comments