11import { BrowserClient } from '@sentry/browser' ;
22import { Hub , makeMain } from '@sentry/hub' ;
3- import { BaggageObj } from '@sentry/types' ;
3+ import type { BaggageObj , BaseTransportOptions , ClientOptions } from '@sentry/types' ;
44import { getGlobalObject , InstrumentHandlerCallback , InstrumentHandlerType } from '@sentry/utils' ;
55import { JSDOM } from 'jsdom' ;
66
@@ -415,7 +415,16 @@ describe('BrowserTracing', () => {
415415 } ) ;
416416 } ) ;
417417
418- describe ( 'using the data' , ( ) => {
418+ describe ( 'using the <meta> tag data' , ( ) => {
419+ beforeEach ( ( ) => {
420+ hub . getClient ( ) ! . getOptions = ( ) => {
421+ return {
422+ release : '1.0.0' ,
423+ environment : 'production' ,
424+ } as ClientOptions < BaseTransportOptions > ;
425+ } ;
426+ } ) ;
427+
419428 it ( 'uses the tracing data for pageload transactions' , ( ) => {
420429 // make sampled false here, so we can see that it's being used rather than the tracesSampleRate-dictated one
421430 document . head . innerHTML =
@@ -439,11 +448,34 @@ describe('BrowserTracing', () => {
439448 expect ( baggage [ 1 ] ) . toEqual ( 'foo=bar' ) ;
440449 } ) ;
441450
451+ it ( 'adds Sentry baggage data to pageload transactions if not present in meta tags' , ( ) => {
452+ // make sampled false here, so we can see that it's being used rather than the tracesSampleRate-dictated one
453+ document . head . innerHTML =
454+ '<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">' +
455+ '<meta name="baggage" content="foo=bar">' ;
456+
457+ // pageload transactions are created as part of the BrowserTracing integration's initialization
458+ createBrowserTracing ( true ) ;
459+ const transaction = getActiveTransaction ( hub ) as IdleTransaction ;
460+ const baggage = transaction . getBaggage ( ) ! ;
461+
462+ expect ( transaction ) . toBeDefined ( ) ;
463+ expect ( transaction . op ) . toBe ( 'pageload' ) ;
464+ expect ( transaction . traceId ) . toEqual ( '12312012123120121231201212312012' ) ;
465+ expect ( transaction . parentSpanId ) . toEqual ( '1121201211212012' ) ;
466+ expect ( transaction . sampled ) . toBe ( false ) ;
467+ expect ( baggage ) . toBeDefined ( ) ;
468+ expect ( baggage [ 0 ] ) . toBeDefined ( ) ;
469+ expect ( baggage [ 0 ] ) . toEqual ( { environment : 'production' , release : '1.0.0' } ) ;
470+ expect ( baggage [ 1 ] ) . toBeDefined ( ) ;
471+ expect ( baggage [ 1 ] ) . toEqual ( 'foo=bar' ) ;
472+ } ) ;
473+
442474 it ( 'ignores the data for navigation transactions' , ( ) => {
443475 mockChangeHistory = ( ) => undefined ;
444476 document . head . innerHTML =
445477 '<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">' +
446- '<meta name="baggage" content="sentry-release=2.1.14,foo=bar ">' ;
478+ '<meta name="baggage" content="sentry-release=2.1.14">' ;
447479
448480 createBrowserTracing ( true ) ;
449481
@@ -455,7 +487,11 @@ describe('BrowserTracing', () => {
455487 expect ( transaction . op ) . toBe ( 'navigation' ) ;
456488 expect ( transaction . traceId ) . not . toEqual ( '12312012123120121231201212312012' ) ;
457489 expect ( transaction . parentSpanId ) . toBeUndefined ( ) ;
458- expect ( baggage ) . toBeUndefined ( ) ;
490+ expect ( baggage ) . toBeDefined ( ) ;
491+ expect ( baggage [ 0 ] ) . toBeDefined ( ) ;
492+ expect ( baggage [ 0 ] ) . toEqual ( { release : '1.0.0' , environment : 'production' } ) ;
493+ expect ( baggage [ 1 ] ) . toBeDefined ( ) ;
494+ expect ( baggage [ 1 ] ) . toEqual ( '' ) ;
459495 } ) ;
460496 } ) ;
461497 } ) ;
0 commit comments