@@ -269,6 +269,75 @@ describe('startSpan', () => {
269
269
expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
270
270
} ) ;
271
271
272
+ describe ( 'handles multiple spans in sequence with a custom scope' , ( ) => {
273
+ it ( 'with parent span' , ( ) => {
274
+ const initialScope = getCurrentScope ( ) ;
275
+
276
+ const manualScope = initialScope . clone ( ) ;
277
+ const parentSpan = new SentrySpan ( { spanId : 'parent-span-id' , sampled : true } ) ;
278
+ _setSpanForScope ( manualScope , parentSpan ) ;
279
+
280
+ startSpan ( { name : 'span 1' , scope : manualScope } , span1 => {
281
+ expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
282
+ expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
283
+ expect ( getActiveSpan ( ) ) . toBe ( span1 ) ;
284
+ expect ( spanToJSON ( span1 ) . parent_span_id ) . toBe ( 'parent-span-id' ) ;
285
+ } ) ;
286
+
287
+ withScope ( manualScope , ( ) => {
288
+ expect ( getActiveSpan ( ) ) . toBe ( parentSpan ) ;
289
+ } ) ;
290
+
291
+ startSpan ( { name : 'span 2' , scope : manualScope } , span2 => {
292
+ expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
293
+ expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
294
+ expect ( getActiveSpan ( ) ) . toBe ( span2 ) ;
295
+ expect ( spanToJSON ( span2 ) . parent_span_id ) . toBe ( 'parent-span-id' ) ;
296
+ } ) ;
297
+
298
+ withScope ( manualScope , ( ) => {
299
+ expect ( getActiveSpan ( ) ) . toBe ( parentSpan ) ;
300
+ } ) ;
301
+
302
+ expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
303
+ expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
304
+ } ) ;
305
+
306
+ it ( 'without parent span' , ( ) => {
307
+ const initialScope = getCurrentScope ( ) ;
308
+ const manualScope = initialScope . clone ( ) ;
309
+
310
+ const traceId = manualScope . getPropagationContext ( ) ?. traceId ;
311
+
312
+ startSpan ( { name : 'span 1' , scope : manualScope } , span1 => {
313
+ expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
314
+ expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
315
+ expect ( getActiveSpan ( ) ) . toBe ( span1 ) ;
316
+ expect ( spanToJSON ( span1 ) . parent_span_id ) . toBe ( undefined ) ;
317
+ expect ( span1 . spanContext ( ) . traceId ) . toBe ( traceId ) ;
318
+ } ) ;
319
+
320
+ withScope ( manualScope , ( ) => {
321
+ expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
322
+ } ) ;
323
+
324
+ startSpan ( { name : 'span 2' , scope : manualScope } , span2 => {
325
+ expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
326
+ expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
327
+ expect ( getActiveSpan ( ) ) . toBe ( span2 ) ;
328
+ expect ( spanToJSON ( span2 ) . parent_span_id ) . toBe ( undefined ) ;
329
+ expect ( span2 . spanContext ( ) . traceId ) . toBe ( traceId ) ;
330
+ } ) ;
331
+
332
+ withScope ( manualScope , ( ) => {
333
+ expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
334
+ } ) ;
335
+
336
+ expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
337
+ expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
338
+ } ) ;
339
+ } ) ;
340
+
272
341
it ( 'allows to pass a parentSpan' , ( ) => {
273
342
const parentSpan = new SentrySpan ( { spanId : 'parent-span-id' , sampled : true , name : 'parent-span' } ) ;
274
343
0 commit comments