@@ -53,6 +53,7 @@ function createEnvironment({ baseEnvironment } = {}) {
53
53
tags : transactionOptions . tags ,
54
54
} ) ;
55
55
this . global . transaction = this . transaction ;
56
+ this . global . Sentry = this . Sentry ;
56
57
57
58
this . Sentry . configureScope ( ( scope ) => scope . setSpan ( this . transaction ) ) ;
58
59
@@ -251,15 +252,20 @@ function createEnvironment({ baseEnvironment } = {}) {
251
252
252
253
// If we are starting a test, let's also make it a transaction so we can see our slowest tests
253
254
if ( spanProps . op === "test" ) {
254
- spans . push (
255
- this . Sentry . startTransaction ( {
256
- ...spanProps ,
257
- op : "jest test" ,
258
- name : spanProps . description ,
259
- description : null ,
260
- tags : this . options . transactionOptions ?. tags ,
261
- } )
262
- ) ;
255
+ const testTransaction = this . Sentry . startTransaction ( {
256
+ ...spanProps ,
257
+ op : "jest test" ,
258
+ name : spanProps . description ,
259
+ description : null ,
260
+ // attach the trace id and span id of parent transaction so they're part of the same trace
261
+ parentSpanId : span [ 0 ] . spanId ,
262
+ traceId : span [ 0 ] . transaction . traceId ,
263
+ tags : this . options . transactionOptions ?. tags ,
264
+ } )
265
+ spans . push ( testTransaction ) ;
266
+
267
+ // ensure that the test transaction is on the scope while it's happening
268
+ this . Sentry . configureScope ( ( scope ) => scope . setSpan ( testTransaction ) ) ;
263
269
}
264
270
265
271
dataStore . set ( testName , spans ) ;
@@ -272,11 +278,7 @@ function createEnvironment({ baseEnvironment } = {}) {
272
278
273
279
if ( name . includes ( "failure" ) ) {
274
280
if ( event . error ) {
275
- const testSpan = spans . find ( span => span . transaction . op === "jest test" )
276
- this . Sentry . configureScope ( scope => {
277
- scope . setSpan ( testSpan . transaction ?? spans [ 0 ] . transaction ) ;
278
- } ) ;
279
- const exception = this . Sentry . captureException ( event . error ) ;
281
+ this . Sentry . captureException ( event . error ) ;
280
282
}
281
283
}
282
284
@@ -289,6 +291,12 @@ function createEnvironment({ baseEnvironment } = {}) {
289
291
}
290
292
291
293
span . finish ( ) ;
294
+
295
+ // if this is finishing a jest test span, then put the test suite transaction
296
+ // back on the scope
297
+ if ( span . op === "jest test" ) {
298
+ this . Sentry . configureScope ( ( scope ) => scope . setSpan ( this . transaction ) ) ;
299
+ }
292
300
} ) ;
293
301
}
294
302
}
0 commit comments