@@ -8,7 +8,10 @@ import {
8
8
setCurrentClient ,
9
9
spanIsSampled ,
10
10
spanToJSON ,
11
+ startInactiveSpan ,
11
12
} from '@sentry/core' ;
13
+ import type { StartSpanOptions } from '@sentry/types' ;
14
+ import { timestampInSeconds } from '@sentry/utils' ;
12
15
import { JSDOM } from 'jsdom' ;
13
16
import { browserTracingIntegration , startBrowserTracingNavigationSpan , startBrowserTracingPageLoadSpan } from '../..' ;
14
17
import { WINDOW } from '../../src/browser/types' ;
@@ -181,6 +184,26 @@ describe('browserTracingIntegration', () => {
181
184
} ) ;
182
185
} ) ;
183
186
187
+ it ( 'trims pageload transactions to the max duration of the transactions children' , ( ) => {
188
+ const client = new TestClient (
189
+ getDefaultClientOptions ( {
190
+ integrations : [ browserTracingIntegration ( ) ] ,
191
+ } ) ,
192
+ ) ;
193
+
194
+ setCurrentClient ( client ) ;
195
+ client . init ( ) ;
196
+
197
+ const pageloadSpan = getActiveSpan ( ) ;
198
+ const childSpan = startInactiveSpan ( { name : 'pageload-child' } ) ;
199
+ const timestamp = timestampInSeconds ( ) ;
200
+
201
+ childSpan ?. end ( timestamp ) ;
202
+ pageloadSpan ?. end ( timestamp + 12345 ) ;
203
+
204
+ expect ( spanToJSON ( pageloadSpan ! ) . timestamp ) . toBe ( timestamp ) ;
205
+ } ) ;
206
+
184
207
describe ( 'startBrowserTracingPageLoadSpan' , ( ) => {
185
208
it ( 'works without integration setup' , ( ) => {
186
209
const client = new TestClient (
@@ -275,6 +298,30 @@ describe('browserTracingIntegration', () => {
275
298
trace_id : expect . any ( String ) ,
276
299
} ) ;
277
300
} ) ;
301
+
302
+ it ( 'calls before beforeStartSpan' , ( ) => {
303
+ const mockBeforeStartSpan = jest . fn ( ( options : StartSpanOptions ) => options ) ;
304
+
305
+ const client = new TestClient (
306
+ getDefaultClientOptions ( {
307
+ tracesSampleRate : 0 ,
308
+ integrations : [
309
+ browserTracingIntegration ( { instrumentPageLoad : false , beforeStartSpan : mockBeforeStartSpan } ) ,
310
+ ] ,
311
+ } ) ,
312
+ ) ;
313
+ setCurrentClient ( client ) ;
314
+ client . init ( ) ;
315
+
316
+ startBrowserTracingPageLoadSpan ( client , { name : 'test span' } ) ;
317
+
318
+ expect ( mockBeforeStartSpan ) . toHaveBeenCalledWith (
319
+ expect . objectContaining ( {
320
+ name : 'test span' ,
321
+ op : 'pageload' ,
322
+ } ) ,
323
+ ) ;
324
+ } ) ;
278
325
} ) ;
279
326
280
327
describe ( 'startBrowserTracingNavigationSpan' , ( ) => {
@@ -371,5 +418,33 @@ describe('browserTracingIntegration', () => {
371
418
trace_id : expect . any ( String ) ,
372
419
} ) ;
373
420
} ) ;
421
+
422
+ it ( 'calls before beforeStartSpan' , ( ) => {
423
+ const mockBeforeStartSpan = jest . fn ( ( options : StartSpanOptions ) => options ) ;
424
+
425
+ const client = new TestClient (
426
+ getDefaultClientOptions ( {
427
+ tracesSampleRate : 0 ,
428
+ integrations : [
429
+ browserTracingIntegration ( {
430
+ instrumentPageLoad : false ,
431
+ instrumentNavigation : false ,
432
+ beforeStartSpan : mockBeforeStartSpan ,
433
+ } ) ,
434
+ ] ,
435
+ } ) ,
436
+ ) ;
437
+ setCurrentClient ( client ) ;
438
+ client . init ( ) ;
439
+
440
+ startBrowserTracingNavigationSpan ( client , { name : 'test span' } ) ;
441
+
442
+ expect ( mockBeforeStartSpan ) . toHaveBeenCalledWith (
443
+ expect . objectContaining ( {
444
+ name : 'test span' ,
445
+ op : 'navigation' ,
446
+ } ) ,
447
+ ) ;
448
+ } ) ;
374
449
} ) ;
375
450
} ) ;
0 commit comments