@@ -8,7 +8,10 @@ import {
88 setCurrentClient ,
99 spanIsSampled ,
1010 spanToJSON ,
11+ startInactiveSpan ,
1112} from '@sentry/core' ;
13+ import type { StartSpanOptions } from '@sentry/types' ;
14+ import { timestampInSeconds } from '@sentry/utils' ;
1215import { JSDOM } from 'jsdom' ;
1316import { browserTracingIntegration , startBrowserTracingNavigationSpan , startBrowserTracingPageLoadSpan } from '../..' ;
1417import { WINDOW } from '../../src/browser/types' ;
@@ -181,6 +184,26 @@ describe('browserTracingIntegration', () => {
181184 } ) ;
182185 } ) ;
183186
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+
184207 describe ( 'startBrowserTracingPageLoadSpan' , ( ) => {
185208 it ( 'works without integration setup' , ( ) => {
186209 const client = new TestClient (
@@ -275,6 +298,30 @@ describe('browserTracingIntegration', () => {
275298 trace_id : expect . any ( String ) ,
276299 } ) ;
277300 } ) ;
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+ } ) ;
278325 } ) ;
279326
280327 describe ( 'startBrowserTracingNavigationSpan' , ( ) => {
@@ -371,5 +418,33 @@ describe('browserTracingIntegration', () => {
371418 trace_id : expect . any ( String ) ,
372419 } ) ;
373420 } ) ;
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+ } ) ;
374449 } ) ;
375450} ) ;
0 commit comments