@@ -274,7 +274,7 @@ describe('Class: Tracer', () => {
274
274
275
275
describe ( 'Method: getSegment' , ( ) => {
276
276
277
- test ( 'when called outside of a namespace or without parent segment, it throws an error' , ( ) => {
277
+ test ( 'when called outside of a namespace or without parent segment, and Tracer is active, it throws an error' , ( ) => {
278
278
279
279
// Prepare
280
280
const tracer : Tracer = new Tracer ( ) ;
@@ -286,7 +286,7 @@ describe('Class: Tracer', () => {
286
286
287
287
} ) ;
288
288
289
- test ( 'when called outside of a namespace or without parent segment , it throws an error' , ( ) => {
289
+ test ( 'when called and no segment is returned, while Tracer is active , it throws an error' , ( ) => {
290
290
291
291
// Prepare
292
292
const tracer : Tracer = new Tracer ( ) ;
@@ -298,7 +298,22 @@ describe('Class: Tracer', () => {
298
298
} ) . toThrow ( 'Failed to get the current sub/segment from the context.' ) ;
299
299
300
300
} ) ;
301
-
301
+
302
+ test ( 'when called outside of a namespace or without parent segment, and Tracer is NOT active, it returns a dummy subsegment' , ( ) => {
303
+
304
+ // Prepare
305
+ delete process . env . AWS_EXECUTION_ENV ; // This will disable the tracer, simulating local execution
306
+ const tracer : Tracer = new Tracer ( ) ;
307
+
308
+ // Act
309
+ const segment = tracer . getSegment ( ) ;
310
+
311
+ // Assess
312
+ expect ( segment ) . toBeInstanceOf ( Subsegment ) ;
313
+ expect ( segment . name ) . toBe ( '## Dummy segment' ) ;
314
+
315
+ } ) ;
316
+
302
317
test ( 'when called within a namespace, it returns the parent segment' , ( ) => {
303
318
304
319
// Prepare
@@ -320,7 +335,7 @@ describe('Class: Tracer', () => {
320
335
} ) ;
321
336
322
337
describe ( 'Method: setSegment' , ( ) => {
323
- test ( 'when called outside of a namespace or without parent segment, it throws an error' , ( ) => {
338
+ test ( 'when called outside of a namespace or without parent segment, and Tracer is enabled, it throws an error' , ( ) => {
324
339
325
340
// Prepare
326
341
const tracer : Tracer = new Tracer ( ) ;
@@ -332,6 +347,22 @@ describe('Class: Tracer', () => {
332
347
} ) . toThrow ( 'No context available. ns.run() or ns.bind() must be called first.' ) ;
333
348
} ) ;
334
349
350
+ test ( 'when called outside of a namespace or without parent segment, and Tracer is NOT enabled, it does nothing' , ( ) => {
351
+
352
+ // Prepare
353
+ delete process . env . AWS_EXECUTION_ENV ; // This will disable the tracer, simulating local execution
354
+ const tracer : Tracer = new Tracer ( ) ;
355
+ const setSegmentSpy = jest . spyOn ( tracer . provider , 'setSegment' ) ;
356
+
357
+ // Act
358
+ const newSubsegment = new Subsegment ( '## foo.bar' ) ;
359
+ tracer . setSegment ( newSubsegment ) ;
360
+
361
+ // Assess
362
+ expect ( setSegmentSpy ) . toBeCalledTimes ( 0 ) ;
363
+
364
+ } ) ;
365
+
335
366
test ( 'when called within a namespace, it sets the segment' , ( ) => {
336
367
337
368
// Prepare
0 commit comments