@@ -780,39 +780,57 @@ describe('startSpanManual', () => {
780780 expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
781781 } ) ;
782782
783- describe ( 'allows to pass a scope' , ( ) => {
783+ describe ( 'starts a span on the fork of a custom scope if passed ' , ( ) => {
784784 it ( 'with parent span' , ( ) => {
785785 const initialScope = getCurrentScope ( ) ;
786786
787- const manualScope = initialScope . clone ( ) ;
788- const parentSpan = new SentrySpan ( { spanId : 'parent-span-id' , sampled : true } ) ;
789- _setSpanForScope ( manualScope , parentSpan ) ;
787+ const customScope = initialScope . clone ( ) ;
788+ customScope . setTag ( 'dogs' , 'great' ) ;
790789
791- let span1 : Span | undefined ;
790+ const parentSpan = new SentrySpan ( { spanId : 'parent-span-id' , sampled : true } ) ;
791+ _setSpanForScope ( customScope , parentSpan ) ;
792792
793- startSpanManual ( { name : 'GET users/[id]' , scope : manualScope } , span => {
794- span1 = span ;
793+ startSpanManual ( { name : 'GET users/[id]' , scope : customScope } , span => {
794+ // current scope is forked from the customScope
795795 expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
796- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
797- expect ( getActiveSpan ( ) ) . toBe ( span ) ;
796+ expect ( getCurrentScope ( ) ) . not . toBe ( customScope ) ;
798797 expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( 'parent-span-id' ) ;
799798
799+ // span is active span
800+ expect ( getActiveSpan ( ) ) . toBe ( span ) ;
801+
800802 span . end ( ) ;
801803
802- // Is still the active span
804+ // span is still the active span (weird but it is what it is)
803805 expect ( getActiveSpan ( ) ) . toBe ( span ) ;
806+
807+ getCurrentScope ( ) . setTag ( 'cats' , 'great' ) ;
808+ customScope . setTag ( 'bears' , 'great' ) ;
809+
810+ expect ( getCurrentScope ( ) . getScopeData ( ) . tags ) . toEqual ( { dogs : 'great' , cats : 'great' } ) ;
811+ expect ( customScope . getScopeData ( ) . tags ) . toEqual ( { dogs : 'great' , bears : 'great' } ) ;
804812 } ) ;
805813
806- startSpanManual ( { name : 'POST users/[id]' , scope : manualScope } , ( span , finish ) => {
814+ expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
815+ expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
816+
817+ startSpanManual ( { name : 'POST users/[id]' , scope : customScope } , ( span , finish ) => {
818+ // current scope is forked from the customScope
807819 expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
808- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
820+ expect ( getCurrentScope ( ) ) . not . toBe ( customScope ) ;
821+ expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( 'parent-span-id' ) ;
822+
823+ // scope data modification from customScope in previous callback is persisted
824+ expect ( getCurrentScope ( ) . getScopeData ( ) . tags ) . toEqual ( { dogs : 'great' , bears : 'great' } ) ;
825+
826+ // span is active span
809827 expect ( getActiveSpan ( ) ) . toBe ( span ) ;
810- expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( span1 ?. spanContext ( ) . spanId ) ;
811828
829+ // calling finish() or span.end() has the same effect
812830 finish ( ) ;
813831
814832 // using finish() resets the scope correctly
815- expect ( getActiveSpan ( ) ) . toBe ( span1 ) ;
833+ expect ( getActiveSpan ( ) ) . toBe ( span ) ;
816834 } ) ;
817835
818836 expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
@@ -824,27 +842,33 @@ describe('startSpanManual', () => {
824842 const manualScope = initialScope . clone ( ) ;
825843
826844 startSpanManual ( { name : 'GET users/[id]' , scope : manualScope } , ( span , finish ) => {
845+ // current scope is forked from the customScope
827846 expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
828- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
847+ expect ( getCurrentScope ( ) ) . not . toBe ( manualScope ) ;
848+ expect ( getCurrentScope ( ) ) . toEqual ( manualScope ) ;
849+
850+ // span is active span and a root span
829851 expect ( getActiveSpan ( ) ) . toBe ( span ) ;
830- expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( undefined ) ;
852+ expect ( getRootSpan ( span ) ) . toBe ( span ) ;
831853
832- finish ( ) ;
854+ span . end ( ) ;
833855
834- // Is still the active span
835- expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
856+ expect ( getActiveSpan ( ) ) . toBe ( span ) ;
836857 } ) ;
837858
838- startSpanManual ( { name : 'GET users/[id]' , scope : manualScope } , ( span , finish ) => {
859+ startSpanManual ( { name : 'POST users/[id]' , scope : manualScope } , ( span , finish ) => {
839860 expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
840- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
861+ expect ( getCurrentScope ( ) ) . not . toBe ( manualScope ) ;
862+ expect ( getCurrentScope ( ) ) . toEqual ( manualScope ) ;
863+
864+ // second span is active span and its own root span
841865 expect ( getActiveSpan ( ) ) . toBe ( span ) ;
842- expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( undefined ) ;
866+ expect ( getRootSpan ( span ) ) . toBe ( span ) ;
843867
844868 finish ( ) ;
845869
846- // using finish() resets the scope correctly
847- expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
870+ // calling finish() or span.end() has the same effect
871+ expect ( getActiveSpan ( ) ) . toBe ( span ) ;
848872 } ) ;
849873
850874 expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
0 commit comments