@@ -15,6 +15,7 @@ import {
1515import * as integrationModule from '../../src/integration' ;
1616import { _INTERNAL_captureLog } from '../../src/logs/internal' ;
1717import { _INTERNAL_captureMetric } from '../../src/metrics/internal' ;
18+ import * as traceModule from '../../src/tracing/trace' ;
1819import { DEFAULT_TRANSPORT_BUFFER_SIZE } from '../../src/transports/base' ;
1920import type { Envelope } from '../../src/types-hoist/envelope' ;
2021import type { ErrorEvent , Event , TransactionEvent } from '../../src/types-hoist/event' ;
@@ -2733,41 +2734,80 @@ describe('Client', () => {
27332734 await expect ( promise ) . rejects . toThrowError ( error ) ;
27342735 } ) ;
27352736
2736- test ( 'accepts isolateTrace option without error' , ( ) => {
2737- const result = 'foo' ;
2738- const callback = vi . fn ( ) . mockReturnValue ( result ) ;
2737+ describe ( 'isolateTrace' , ( ) => {
2738+ const startNewTraceSpy = vi . spyOn ( traceModule , 'startNewTrace' ) . mockImplementation ( cb => cb ( ) ) ;
27392739
2740- const returnedResult = withMonitor ( 'test-monitor' , callback , {
2741- schedule : { type : 'crontab' , value : '* * * * *' } ,
2742- isolateTrace : true ,
2740+ beforeEach ( ( ) => {
2741+ startNewTraceSpy . mockClear ( ) ;
27432742 } ) ;
27442743
2745- expect ( returnedResult ) . toBe ( result ) ;
2746- expect ( callback ) . toHaveBeenCalledTimes ( 1 ) ;
2747- } ) ;
2744+ it ( 'starts a new trace when isolateTrace is true (sync)' , ( ) => {
2745+ const result = 'foo' ;
2746+ const callback = vi . fn ( ) . mockReturnValue ( result ) ;
27482747
2749- test ( 'works with isolateTrace set to false' , ( ) => {
2750- const result = 'foo' ;
2751- const callback = vi . fn ( ) . mockReturnValue ( result ) ;
2748+ const returnedResult = withMonitor ( 'test-monitor' , callback , {
2749+ schedule : { type : 'crontab' , value : '* * * * *' } ,
2750+ isolateTrace : true ,
2751+ } ) ;
27522752
2753- const returnedResult = withMonitor ( 'test-monitor' , callback , {
2754- schedule : { type : 'crontab' , value : '* * * * *' } ,
2755- isolateTrace : false ,
2753+ expect ( returnedResult ) . toBe ( result ) ;
2754+ expect ( callback ) . toHaveBeenCalledTimes ( 1 ) ;
2755+ expect ( startNewTraceSpy ) . toHaveBeenCalledTimes ( 1 ) ;
27562756 } ) ;
27572757
2758- expect ( returnedResult ) . toBe ( result ) ;
2759- expect ( callback ) . toHaveBeenCalledTimes ( 1 ) ;
2760- } ) ;
2758+ it ( 'starts a new trace when isolateTrace is true (async)' , async ( ) => {
2759+ const result = 'foo' ;
2760+ const callback = vi . fn ( ) . mockResolvedValue ( result ) ;
27612761
2762- test ( 'handles isolateTrace with asynchronous operations' , async ( ) => {
2763- const result = 'foo' ;
2764- const callback = vi . fn ( ) . mockResolvedValue ( result ) ;
2762+ const promise = withMonitor ( 'test-monitor' , callback , {
2763+ schedule : { type : 'crontab' , value : '* * * * *' } ,
2764+ isolateTrace : true ,
2765+ } ) ;
2766+ await expect ( promise ) . resolves . toEqual ( result ) ;
2767+ expect ( callback ) . toHaveBeenCalledTimes ( 1 ) ;
2768+ expect ( startNewTraceSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2769+ } ) ;
27652770
2766- const promise = withMonitor ( 'test-monitor' , callback , {
2767- schedule : { type : 'crontab' , value : '* * * * *' } ,
2768- isolateTrace : true ,
2771+ it ( "doesn't start a new trace when isolateTrace is false (sync)" , ( ) => {
2772+ const result = 'foo' ;
2773+ const callback = vi . fn ( ) . mockReturnValue ( result ) ;
2774+
2775+ const returnedResult = withMonitor ( 'test-monitor' , callback , {
2776+ schedule : { type : 'crontab' , value : '* * * * *' } ,
2777+ isolateTrace : false ,
2778+ } ) ;
2779+
2780+ expect ( returnedResult ) . toBe ( result ) ;
2781+ expect ( callback ) . toHaveBeenCalledTimes ( 1 ) ;
2782+ expect ( startNewTraceSpy ) . not . toHaveBeenCalled ( ) ;
2783+ } ) ;
2784+
2785+ it ( "doesn't start a new trace when isolateTrace is false (async)" , async ( ) => {
2786+ const result = 'foo' ;
2787+ const callback = vi . fn ( ) . mockResolvedValue ( result ) ;
2788+
2789+ const promise = withMonitor ( 'test-monitor' , callback , {
2790+ schedule : { type : 'crontab' , value : '* * * * *' } ,
2791+ isolateTrace : false ,
2792+ } ) ;
2793+
2794+ await expect ( promise ) . resolves . toEqual ( result ) ;
2795+ expect ( callback ) . toHaveBeenCalledTimes ( 1 ) ;
2796+ expect ( startNewTraceSpy ) . not . toHaveBeenCalled ( ) ;
2797+ } ) ;
2798+
2799+ it ( "doesn't start a new trace by default" , ( ) => {
2800+ const result = 'foo' ;
2801+ const callback = vi . fn ( ) . mockReturnValue ( result ) ;
2802+
2803+ const returnedResult = withMonitor ( 'test-monitor' , callback , {
2804+ schedule : { type : 'crontab' , value : '* * * * *' } ,
2805+ } ) ;
2806+
2807+ expect ( returnedResult ) . toBe ( result ) ;
2808+ expect ( callback ) . toHaveBeenCalledTimes ( 1 ) ;
2809+ expect ( startNewTraceSpy ) . not . toHaveBeenCalled ( ) ;
27692810 } ) ;
2770- await expect ( promise ) . resolves . toEqual ( result ) ;
27712811 } ) ;
27722812 } ) ;
27732813
0 commit comments