1- import type { Span } from '@sentry/types' ;
21import { Hub , addTracingExtensions , getCurrentScope , makeMain } from '../../../src' ;
3- import { continueTrace , startInactiveSpan , startSpan , startSpanManual } from '../../../src/tracing' ;
2+ import { Scope } from '../../../src/scope' ;
3+ import { Span , continueTrace , startInactiveSpan , startSpan , startSpanManual } from '../../../src/tracing' ;
44import { TestClient , getDefaultTestClientOptions } from '../../mocks/client' ;
55
66beforeAll ( ( ) => {
@@ -81,18 +81,6 @@ describe('startSpan', () => {
8181 expect ( ref . status ) . toEqual ( isError ? 'internal_error' : undefined ) ;
8282 } ) ;
8383
84- it ( 'creates & finishes span' , async ( ) => {
85- let _span : Span | undefined ;
86- startSpan ( { name : 'GET users/[id]' } , span => {
87- expect ( span ) . toBeDefined ( ) ;
88- expect ( span ?. endTimestamp ) . toBeUndefined ( ) ;
89- _span = span ;
90- } ) ;
91-
92- expect ( _span ) . toBeDefined ( ) ;
93- expect ( _span ?. endTimestamp ) . toBeDefined ( ) ;
94- } ) ;
95-
9684 it ( 'allows traceparent information to be overriden' , async ( ) => {
9785 let ref : any = undefined ;
9886 client . on ( 'finishTransaction' , transaction => {
@@ -160,14 +148,6 @@ describe('startSpan', () => {
160148 expect ( ref . spanRecorder . spans [ 1 ] . status ) . toEqual ( isError ? 'internal_error' : undefined ) ;
161149 } ) ;
162150
163- it ( 'allows to pass a `startTime`' , ( ) => {
164- const start = startSpan ( { name : 'outer' , startTime : [ 1234 , 0 ] } , span => {
165- return span ?. startTimestamp ;
166- } ) ;
167-
168- expect ( start ) . toEqual ( 1234 ) ;
169- } ) ;
170-
171151 it ( 'allows for span to be mutated' , async ( ) => {
172152 let ref : any = undefined ;
173153 client . on ( 'finishTransaction' , transaction => {
@@ -189,18 +169,57 @@ describe('startSpan', () => {
189169 expect ( ref . spanRecorder . spans ) . toHaveLength ( 2 ) ;
190170 expect ( ref . spanRecorder . spans [ 1 ] . op ) . toEqual ( 'db.query' ) ;
191171 } ) ;
172+ } ) ;
192173
193- it ( 'forks the scope' , ( ) => {
194- const initialScope = getCurrentScope ( ) ;
174+ it ( 'creates & finishes span' , async ( ) => {
175+ let _span : Span | undefined ;
176+ startSpan ( { name : 'GET users/[id]' } , span => {
177+ expect ( span ) . toBeDefined ( ) ;
178+ expect ( span ?. endTimestamp ) . toBeUndefined ( ) ;
179+ _span = span as Span ;
180+ } ) ;
195181
196- startSpan ( { name : 'GET users/[id]' } , span => {
197- expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
198- expect ( getCurrentScope ( ) . getSpan ( ) ) . toBe ( span ) ;
199- } ) ;
182+ expect ( _span ) . toBeDefined ( ) ;
183+ expect ( _span ?. endTimestamp ) . toBeDefined ( ) ;
184+ } ) ;
200185
201- expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
202- expect ( initialScope . getSpan ( ) ) . toBe ( undefined ) ;
186+ it ( 'allows to pass a `startTime`' , ( ) => {
187+ const start = startSpan ( { name : 'outer' , startTime : [ 1234 , 0 ] } , span => {
188+ return span ?. startTimestamp ;
203189 } ) ;
190+
191+ expect ( start ) . toEqual ( 1234 ) ;
192+ } ) ;
193+
194+ it ( 'forks the scope' , ( ) => {
195+ const initialScope = getCurrentScope ( ) ;
196+
197+ startSpan ( { name : 'GET users/[id]' } , span => {
198+ expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
199+ expect ( getCurrentScope ( ) . getSpan ( ) ) . toBe ( span ) ;
200+ } ) ;
201+
202+ expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
203+ expect ( initialScope . getSpan ( ) ) . toBe ( undefined ) ;
204+ } ) ;
205+
206+ it ( 'allows to pass a scope' , ( ) => {
207+ const initialScope = getCurrentScope ( ) ;
208+
209+ const manualScope = new Scope ( ) ;
210+ const parentSpan = new Span ( { spanId : 'parent-span-id' } ) ;
211+ manualScope . setSpan ( parentSpan ) ;
212+
213+ startSpan ( { name : 'GET users/[id]' , scope : manualScope } , span => {
214+ expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
215+ expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
216+ expect ( getCurrentScope ( ) . getSpan ( ) ) . toBe ( span ) ;
217+
218+ expect ( span ?. parentSpanId ) . toBe ( 'parent-span-id' ) ;
219+ } ) ;
220+
221+ expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
222+ expect ( initialScope . getSpan ( ) ) . toBe ( undefined ) ;
204223 } ) ;
205224} ) ;
206225
@@ -231,6 +250,29 @@ describe('startSpanManual', () => {
231250 expect ( initialScope . getSpan ( ) ) . toBe ( undefined ) ;
232251 } ) ;
233252
253+ it ( 'allows to pass a scope' , ( ) => {
254+ const initialScope = getCurrentScope ( ) ;
255+
256+ const manualScope = new Scope ( ) ;
257+ const parentSpan = new Span ( { spanId : 'parent-span-id' } ) ;
258+ manualScope . setSpan ( parentSpan ) ;
259+
260+ startSpanManual ( { name : 'GET users/[id]' , scope : manualScope } , ( span , finish ) => {
261+ expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
262+ expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
263+ expect ( getCurrentScope ( ) . getSpan ( ) ) . toBe ( span ) ;
264+ expect ( span ?. parentSpanId ) . toBe ( 'parent-span-id' ) ;
265+
266+ finish ( ) ;
267+
268+ // Is still the active span
269+ expect ( getCurrentScope ( ) . getSpan ( ) ) . toBe ( span ) ;
270+ } ) ;
271+
272+ expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
273+ expect ( initialScope . getSpan ( ) ) . toBe ( undefined ) ;
274+ } ) ;
275+
234276 it ( 'allows to pass a `startTime`' , ( ) => {
235277 const start = startSpanManual ( { name : 'outer' , startTime : [ 1234 , 0 ] } , span => {
236278 span ?. end ( ) ;
@@ -266,6 +308,24 @@ describe('startInactiveSpan', () => {
266308 expect ( initialScope . getSpan ( ) ) . toBeUndefined ( ) ;
267309 } ) ;
268310
311+ it ( 'allows to pass a scope' , ( ) => {
312+ const initialScope = getCurrentScope ( ) ;
313+
314+ const manualScope = new Scope ( ) ;
315+ const parentSpan = new Span ( { spanId : 'parent-span-id' } ) ;
316+ manualScope . setSpan ( parentSpan ) ;
317+
318+ const span = startInactiveSpan ( { name : 'GET users/[id]' , scope : manualScope } ) ;
319+
320+ expect ( span ) . toBeDefined ( ) ;
321+ expect ( span ?. parentSpanId ) . toBe ( 'parent-span-id' ) ;
322+ expect ( initialScope . getSpan ( ) ) . toBeUndefined ( ) ;
323+
324+ span ?. end ( ) ;
325+
326+ expect ( initialScope . getSpan ( ) ) . toBeUndefined ( ) ;
327+ } ) ;
328+
269329 it ( 'allows to pass a `startTime`' , ( ) => {
270330 const span = startInactiveSpan ( { name : 'outer' , startTime : [ 1234 , 0 ] } ) ;
271331 expect ( span ?. startTimestamp ) . toEqual ( 1234 ) ;
0 commit comments