@@ -11,6 +11,8 @@ import {
1111 fill ,
1212 markFunctionWrapped ,
1313 objectify ,
14+ unwrapMethod ,
15+ wrapMethod ,
1416} from '../../../src/utils/object' ;
1517import { testOnlyIfNodeVersionAtLeast } from '../../testutils' ;
1618
@@ -455,3 +457,38 @@ describe('markFunctionWrapped', () => {
455457 expect ( originalFunc ) . not . toHaveBeenCalled ( ) ;
456458 } ) ;
457459} ) ;
460+
461+ describe ( 'unwrapMethod, wrapMethod' , ( ) => {
462+ it ( 'can wrap a method on an object and unwrap it later' , ( ) => {
463+ const wrapped = ( ) => { } ;
464+ const original = ( ) => { } ;
465+ const obj = { m : original } ;
466+ wrapMethod ( obj , 'm' , wrapped ) ;
467+ expect ( obj . m ) . toBe ( wrapped ) ;
468+ expect ( ( obj . m as WrappedFunction ) . __sentry_original__ ) . toBe ( original ) ;
469+ unwrapMethod ( obj , 'm' ) ;
470+ expect ( obj . m ) . toBe ( original ) ;
471+ } ) ;
472+
473+ it ( 'throws if misused' , ( ) => {
474+ const wrapped = ( ) => { } ;
475+ const original = ( ) => { } ;
476+ const obj = { m : original } ;
477+ wrapMethod ( obj , 'm' , wrapped ) ;
478+ expect ( ( ) => {
479+ //@ts -expect-error verify type checking prevents this mistake
480+ wrapMethod ( obj , 'foo' , wrapped ) ;
481+ } ) . toThrowError ( 'Cannot wrap method: foo is not a function' ) ;
482+ expect ( ( ) => {
483+ //@ts -expect-error verify type checking prevents this mistake
484+ unwrapMethod ( obj , 'foo' ) ;
485+ } ) . toThrowError ( 'Cannot unwrap method: foo is not a function' ) ;
486+ expect ( ( ) => {
487+ wrapMethod ( obj , 'm' , wrapped ) ;
488+ } ) . toThrowError ( 'Attempting to wrap method m multiple times' ) ;
489+ unwrapMethod ( obj , 'm' ) ;
490+ expect ( ( ) => {
491+ unwrapMethod ( obj , 'm' ) ;
492+ } ) . toThrowError ( 'Method m is not wrapped, and cannot be unwrapped' ) ;
493+ } ) ;
494+ } ) ;
0 commit comments