11import { describe , expect , it , vi } from 'vitest' ;
2+ import type { StreamedSpanJSON } from '../../../../src' ;
23import {
34 captureSpan ,
45 SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT ,
@@ -20,6 +21,7 @@ import {
2021 withScope ,
2122 withStreamedSpan ,
2223} from '../../../../src' ;
24+ import { safeSetSpanJSONAttributes } from '../../../../src/tracing/spans/captureSpan' ;
2325import { getDefaultTestClientOptions , TestClient } from '../../../mocks/client' ;
2426
2527describe ( 'captureSpan' , ( ) => {
@@ -414,3 +416,49 @@ describe('captureSpan', () => {
414416 } ) ;
415417 } ) ;
416418} ) ;
419+
420+ describe ( 'safeSetSpanJSONAttributes' , ( ) => {
421+ it ( 'sets attributes that do not exist' , ( ) => {
422+ const spanJSON = { attributes : { a : 1 , b : 2 } } ;
423+
424+ // @ts -expect-error - only passing a partial object for this test
425+ safeSetSpanJSONAttributes ( spanJSON , { c : 3 } ) ;
426+
427+ expect ( spanJSON . attributes ) . toEqual ( { a : 1 , b : 2 , c : 3 } ) ;
428+ } ) ;
429+
430+ it ( "doesn't set attributes that already exist" , ( ) => {
431+ const spanJSON = { attributes : { a : 1 , b : 2 } } ;
432+ // @ts -expect-error - only passing a partial object for this test
433+ safeSetSpanJSONAttributes ( spanJSON , { a : 3 } ) ;
434+
435+ expect ( spanJSON . attributes ) . toEqual ( { a : 1 , b : 2 } ) ;
436+ } ) ;
437+
438+ it . each ( [ null , undefined ] ) ( "doesn't overwrite attributes previously set to %s" , val => {
439+ const spanJSON = { attributes : { a : val , b : 2 } } ;
440+
441+ // @ts -expect-error - only passing a partial object for this test
442+ safeSetSpanJSONAttributes ( spanJSON , { a : 1 } ) ;
443+
444+ expect ( spanJSON . attributes ) . toEqual ( { a : val , b : 2 } ) ;
445+ } ) ;
446+
447+ it ( "doesn't overwrite falsy attribute values (%s)" , ( ) => {
448+ const spanJSON = { attributes : { a : false , b : '' , c : 0 } } ;
449+
450+ // @ts -expect-error - only passing a partial object for this test
451+ safeSetSpanJSONAttributes ( spanJSON , { a : 1 , b : 'test' , c : 1 } ) ;
452+
453+ expect ( spanJSON . attributes ) . toEqual ( { a : false , b : '' , c : 0 } ) ;
454+ } ) ;
455+
456+ it ( 'handles an undefined attributes property' , ( ) => {
457+ const spanJSON : Partial < StreamedSpanJSON > = { } ;
458+
459+ // @ts -expect-error - only passing a partial object for this test
460+ safeSetSpanJSONAttributes ( spanJSON , { a : 1 } ) ;
461+
462+ expect ( spanJSON . attributes ) . toEqual ( { a : 1 } ) ;
463+ } ) ;
464+ } ) ;
0 commit comments