@@ -20,22 +20,27 @@ describe('trace decorator', () => {
2020 errors : expect . anything ( )
2121 } ;
2222
23- describe ( 'Synchronous functions' , ( ) => {
24- const helloWorldTraceHandlerMock = jest . fn ( ) ;
23+ describe ( 'Tracing synchronous functions' , ( ) => {
24+ const traceHandlerMock = jest . fn ( ) ;
2525
2626 class SyncClass {
27- @trace ( helloWorldTraceHandlerMock )
27+ greetingFrom = [ 'hello from class' ] ;
28+
29+ @trace ( function ( ...args ) {
30+ this . greetingFrom . push ( 'hello from trace handler' ) ;
31+ traceHandlerMock ( ...args ) ;
32+ expect ( this . greetingFrom ) . toEqual ( [ 'hello from class' , 'hello from method' , 'hello from trace handler' ] ) ;
33+ } )
2834 helloWorld ( ) : string {
35+ this . greetingFrom . push ( 'hello from method' ) ;
2936 return 'Hello World' ;
3037 }
3138 }
3239
33- it ( 'should trace a synchronous function' , ( ) => {
40+ it ( 'should trace a synchronous function and verify context ' , ( ) => {
3441 const instance = new SyncClass ( ) ;
35- const response = instance . helloWorld ( ) ;
36-
37- expect ( response ) . toEqual ( 'Hello World' ) ;
38- expect ( helloWorldTraceHandlerMock ) . toHaveBeenCalledWith (
42+ expect ( instance . helloWorld ( ) ) . toBe ( 'Hello World' ) ;
43+ expect ( traceHandlerMock ) . toHaveBeenCalledWith (
3944 expect . objectContaining ( {
4045 metadata : expect . objectContaining ( {
4146 class : 'SyncClass' ,
@@ -52,11 +57,18 @@ describe('trace decorator', () => {
5257 } ) ;
5358
5459 describe ( 'Asynchronous functions' , ( ) => {
55- const helloWorldAsyncTraceHandlerMock = jest . fn ( ) ;
60+ const asyncTraceHandlerMock = jest . fn ( ) ;
5661
5762 class AsyncClass {
58- @trace ( helloWorldAsyncTraceHandlerMock )
63+ greetingFrom = [ 'hello from class' ] ;
64+
65+ @trace ( function ( ...args ) {
66+ this . greetingFrom . push ( 'hello from async trace handler' ) ;
67+ asyncTraceHandlerMock ( ...args ) ;
68+ expect ( this . greetingFrom ) . toEqual ( [ 'hello from class' , 'hello from async method' , 'hello from async trace handler' ] ) ;
69+ } )
5970 async helloWorldAsync ( ) : Promise < string > {
71+ this . greetingFrom . push ( 'hello from async method' ) ;
6072 return 'Hello World async' ;
6173 }
6274 }
@@ -66,7 +78,7 @@ describe('trace decorator', () => {
6678 const response = await instance . helloWorldAsync ( ) ;
6779
6880 expect ( response ) . toEqual ( 'Hello World async' ) ;
69- expect ( helloWorldAsyncTraceHandlerMock ) . toHaveBeenCalledWith (
81+ expect ( asyncTraceHandlerMock ) . toHaveBeenCalledWith (
7082 expect . objectContaining ( {
7183 metadata : expect . objectContaining ( {
7284 class : 'AsyncClass' ,
@@ -126,13 +138,21 @@ describe('trace decorator', () => {
126138
127139 @trace ( traceHandlerFetchDataMock , traceContextFetchData )
128140 @empty ( )
129- async fetchDataFunctionOnAnotherDecorator ( url : string , traceContext : Record < string , unknown > = { } ) : Promise < { data : string } > {
141+ async fetchDataFunctionOnAnotherDecorator (
142+ url : string ,
143+ traceContext : Record < string , unknown > = { }
144+ ) : Promise < {
145+ data : string ;
146+ } > {
130147 return this . fetchDataFunction ( url , traceContext ) ;
131148 }
132149
133150 @trace ( traceHandlerFetchDataMock , traceContextFetchData , { errorStrategy : 'catch' } )
134151 @empty ( )
135- async fetchDataFunctionOnAnotherDecoratorCoughtError ( url : string , traceContext : Record < string , unknown > = { } ) : Promise < { data : string } > {
152+ async fetchDataFunctionOnAnotherDecoratorCoughtError (
153+ url : string ,
154+ traceContext : Record < string , unknown > = { }
155+ ) : Promise < { data : string } > {
136156 return this . fetchDataFunction ( url , traceContext ) ;
137157 }
138158 }
@@ -245,10 +265,10 @@ describe('trace decorator', () => {
245265 expect ( response ) . toEqual ( 0.5 ) ;
246266 } ) ;
247267
248-
249268 it ( 'should async trace error divisionFunctionOnAnotherDecorator' , async ( ) => {
250269 expect ( ( ) => new MyClass ( ) . divisionFunctionOnAnotherDecorator ( 1 , 0 ) ) . toThrow ( 'Throwing because division by zero is not allowed.' ) ;
251- expect ( traceHandlerDivisionMock ) . toHaveBeenNthCalledWith ( 2 ,
270+ expect ( traceHandlerDivisionMock ) . toHaveBeenNthCalledWith (
271+ 2 ,
252272 expect . objectContaining ( {
253273 metadata : expect . objectContaining ( {
254274 class : 'MyClass' ,
@@ -288,7 +308,6 @@ describe('trace decorator', () => {
288308 expect ( response ) . toMatchObject ( { data : 'Success' } ) ;
289309 } ) ;
290310
291-
292311 it ( 'should async trace error fetchDataFunctionOnAnotherDecorator' , async ( ) => {
293312 await expect ( new MyClass ( ) . fetchDataFunctionOnAnotherDecorator ( 'invalid-url' ) ) . rejects . toThrow ( 'Invalid URL provided.' ) ;
294313 expect ( traceHandlerFetchDataMock ) . toHaveBeenNthCalledWith (
@@ -311,7 +330,7 @@ describe('trace decorator', () => {
311330 } ) ;
312331
313332 it ( 'should async trace error fetchDataFunctionOnAnotherDecorator cought' , async ( ) => {
314- const response = await new MyClass ( ) . fetchDataFunctionOnAnotherDecoratorCoughtError ( 'invalid-url' ) ;
333+ const response = await new MyClass ( ) . fetchDataFunctionOnAnotherDecoratorCoughtError ( 'invalid-url' ) ;
315334 expect ( traceHandlerFetchDataMock ) . toHaveBeenNthCalledWith (
316335 2 ,
317336 expect . objectContaining ( {
@@ -330,7 +349,7 @@ describe('trace decorator', () => {
330349 } )
331350 ) ;
332351
333- expect ( response ) . toMatchObject ( [ { ' code' : undefined , ' message' : 'Invalid URL provided.' , ' name' : 'Error' } ] ) ;
352+ expect ( response ) . toMatchObject ( [ { code : undefined , message : 'Invalid URL provided.' , name : 'Error' } ] ) ;
334353 } ) ;
335354 } ) ;
336355} ) ;
0 commit comments