@@ -439,26 +439,42 @@ describe('Class: Logger', () => {
439
439
440
440
describe ( 'Method: addContext' , ( ) => {
441
441
442
+ const context1 = {
443
+ callbackWaitsForEmptyEventLoop : true ,
444
+ functionVersion : '$LATEST' ,
445
+ functionName : 'foo-bar-function-with-cold-start' ,
446
+ memoryLimitInMB : '128' ,
447
+ logGroupName : '/aws/lambda/foo-bar-function-with-cold-start' ,
448
+ logStreamName : '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456' ,
449
+ invokedFunctionArn : 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function-with-cold-start' ,
450
+ awsRequestId : 'c6af9ac6-7b61-11e6-9a41-93e812345678' ,
451
+ getRemainingTimeInMillis : ( ) => 1234 ,
452
+ done : ( ) => console . log ( 'Done!' ) ,
453
+ fail : ( ) => console . log ( 'Failed!' ) ,
454
+ succeed : ( ) => console . log ( 'Succeeded!' ) ,
455
+ } ;
456
+ const context2 = {
457
+ callbackWaitsForEmptyEventLoop : true ,
458
+ functionVersion : '$LATEST' ,
459
+ functionName : 'foo-bar-function-with-cold-start' ,
460
+ memoryLimitInMB : '128' ,
461
+ logGroupName : '/aws/lambda/foo-bar-function-with-cold-start' ,
462
+ logStreamName : '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456' ,
463
+ invokedFunctionArn : 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function-with-cold-start' ,
464
+ awsRequestId : 'd40c98a9-91c4-478c-a179-433c4b978289' ,
465
+ getRemainingTimeInMillis : ( ) => 1234 ,
466
+ done : ( ) => console . log ( 'Done!' ) ,
467
+ fail : ( ) => console . log ( 'Failed!' ) ,
468
+ succeed : ( ) => console . log ( 'Succeeded!' ) ,
469
+ } ;
470
+
442
471
test ( 'when called during a COLD START invocation, it populates the logger\'s PowertoolLogData object with coldstart set to true' , ( ) => {
443
472
444
473
// Prepare
445
474
const logger = new Logger ( ) ;
446
475
447
476
// Act
448
- logger . addContext ( {
449
- callbackWaitsForEmptyEventLoop : true ,
450
- functionVersion : '$LATEST' ,
451
- functionName : 'foo-bar-function-with-cold-start' ,
452
- memoryLimitInMB : '128' ,
453
- logGroupName : '/aws/lambda/foo-bar-function-with-cold-start' ,
454
- logStreamName : '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456' ,
455
- invokedFunctionArn : 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function-with-cold-start' ,
456
- awsRequestId : 'c6af9ac6-7b61-11e6-9a41-93e812345678' ,
457
- getRemainingTimeInMillis : ( ) => 1234 ,
458
- done : ( ) => console . log ( 'Done!' ) ,
459
- fail : ( ) => console . log ( 'Failed!' ) ,
460
- succeed : ( ) => console . log ( 'Succeeded!' ) ,
461
- } ) ;
477
+ logger . addContext ( context1 ) ;
462
478
463
479
// Assess
464
480
expect ( logger ) . toEqual ( {
@@ -492,6 +508,47 @@ describe('Class: Logger', () => {
492
508
} ) ;
493
509
} ) ;
494
510
511
+ test ( 'when called with another request id, it sets cold start to false' , ( ) => {
512
+
513
+ // Prepare
514
+ const logger = new Logger ( ) ;
515
+
516
+ // Act
517
+ logger . addContext ( context1 ) ; // first invocation
518
+ logger . addContext ( context2 ) ; // second invocation
519
+
520
+ // Assess
521
+ expect ( logger ) . toEqual (
522
+ expect . objectContaining ( {
523
+ powertoolLogData : expect . objectContaining ( {
524
+ lambdaContext : expect . objectContaining ( {
525
+ coldStart : false
526
+ } )
527
+ } )
528
+ } )
529
+ ) ;
530
+ } ) ;
531
+
532
+ test ( 'when called multiple times, it obeys the newest context values' , ( ) => {
533
+
534
+ // Prepare
535
+ const logger = new Logger ( ) ;
536
+
537
+ // Act
538
+ logger . addContext ( context1 ) ; // first invocation
539
+ logger . addContext ( context2 ) ; // second invocation
540
+
541
+ // Assess
542
+ expect ( logger ) . toEqual (
543
+ expect . objectContaining ( {
544
+ powertoolLogData : expect . objectContaining ( {
545
+ lambdaContext : expect . objectContaining ( {
546
+ awsRequestId : context2 . awsRequestId ,
547
+ } )
548
+ } )
549
+ } )
550
+ ) ;
551
+ } ) ;
495
552
} ) ;
496
553
497
554
describe ( 'Method: appendKeys' , ( ) => {
@@ -523,6 +580,47 @@ describe('Class: Logger', () => {
523
580
} ,
524
581
} ) ) ;
525
582
} ) ;
583
+
584
+ test ( 'user-provided attribute object is not mutated' , ( ) => {
585
+
586
+ // Prepare
587
+ const logger = new Logger ( ) ;
588
+ const attributes1 = { keyOne : 'abc' } ;
589
+ const attributes2 = { keyTwo : 'def' } ;
590
+
591
+ // Act
592
+ logger . appendKeys ( attributes1 ) ;
593
+ logger . appendKeys ( attributes2 ) ;
594
+
595
+ // Assess
596
+ expect ( attributes1 ) . toEqual ( { keyOne : 'abc' } ) ;
597
+ expect ( attributes2 ) . toEqual ( { keyTwo : 'def' } ) ;
598
+ } ) ;
599
+
600
+ test ( 'when called multiple times with same key, the last value overrides earlier values' , ( ) => {
601
+
602
+ // Prepare
603
+ const logger = new Logger ( ) ;
604
+
605
+ // Act
606
+ logger . appendKeys ( {
607
+ keyOne : 'abc' ,
608
+ duplicateKey : 'one'
609
+ } ) ;
610
+ logger . appendKeys ( {
611
+ keyTwo : 'def' ,
612
+ duplicateKey : 'two'
613
+ } ) ;
614
+
615
+ // Assess
616
+ expect ( logger ) . toEqual ( expect . objectContaining ( {
617
+ persistentLogAttributes : {
618
+ keyOne : 'abc' ,
619
+ keyTwo : 'def' ,
620
+ duplicateKey : 'two'
621
+ }
622
+ } ) ) ;
623
+ } ) ;
526
624
} ) ;
527
625
528
626
describe ( 'Method: createChild' , ( ) => {
0 commit comments