@@ -439,26 +439,28 @@ describe('Class: Logger', () => {
439439
440440 describe ( 'Method: addContext' , ( ) => {
441441
442+ const baseContext = {
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+
442457 test ( 'when called during a COLD START invocation, it populates the logger\'s PowertoolLogData object with coldstart set to true' , ( ) => {
443458
444459 // Prepare
445460 const logger = new Logger ( ) ;
446461
447462 // 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- } ) ;
463+ logger . addContext ( baseContext ) ;
462464
463465 // Assess
464466 expect ( logger ) . toEqual ( {
@@ -492,6 +494,44 @@ describe('Class: Logger', () => {
492494 } ) ;
493495 } ) ;
494496
497+ test ( 'when called with a context object, the object is not mutated' , ( ) => {
498+
499+ // Prepare
500+ const logger = new Logger ( ) ;
501+ const context1 = { ...baseContext , awsRequestId : 'c6af9ac6-7b61-11e6-9a41-93e812345678' } ;
502+ const context2 = { ...baseContext , awsRequestId : 'd40c98a9-91c4-478c-a179-433c4b978289' } ;
503+
504+ // Act
505+ logger . addContext ( context1 ) ;
506+ logger . addContext ( context2 ) ;
507+
508+ // Assess
509+ expect ( context1 . awsRequestId ) . toEqual ( 'c6af9ac6-7b61-11e6-9a41-93e812345678' ) ;
510+ expect ( context2 . awsRequestId ) . toEqual ( 'd40c98a9-91c4-478c-a179-433c4b978289' ) ;
511+ } ) ;
512+
513+ test ( 'when called multiple times, the newer values override earlier values' , ( ) => {
514+
515+ // Prepare
516+ const logger = new Logger ( ) ;
517+ const context1 = { ...baseContext , awsRequestId : 'c6af9ac6-7b61-11e6-9a41-93e812345678' } ;
518+ const context2 = { ...baseContext , awsRequestId : 'd40c98a9-91c4-478c-a179-433c4b978289' } ;
519+
520+ // Act
521+ logger . addContext ( context1 ) ;
522+ logger . addContext ( context2 ) ;
523+
524+ // Assess
525+ expect ( logger ) . toEqual (
526+ expect . objectContaining ( {
527+ powertoolLogData : expect . objectContaining ( {
528+ lambdaContext : expect . objectContaining ( {
529+ awsRequestId : context2 . awsRequestId ,
530+ } )
531+ } )
532+ } )
533+ ) ;
534+ } ) ;
495535 } ) ;
496536
497537 describe ( 'Method: appendKeys' , ( ) => {
@@ -523,6 +563,43 @@ describe('Class: Logger', () => {
523563 } ,
524564 } ) ) ;
525565 } ) ;
566+
567+ test ( 'when called with user-provided attribute objects, the objects are not mutated' , ( ) => {
568+
569+ // Prepare
570+ const logger = new Logger ( ) ;
571+ const attributes1 = { keyOne : 'abc' } ;
572+ const attributes2 = { keyTwo : 'def' } ;
573+
574+ // Act
575+ logger . appendKeys ( attributes1 ) ;
576+ logger . appendKeys ( attributes2 ) ;
577+
578+ // Assess
579+ expect ( attributes1 ) . toEqual ( { keyOne : 'abc' } ) ;
580+ expect ( attributes2 ) . toEqual ( { keyTwo : 'def' } ) ;
581+ } ) ;
582+
583+ test ( 'when called multiple times, the newer values override earlier values' , ( ) => {
584+
585+ // Prepare
586+ const logger = new Logger ( ) ;
587+
588+ // Act
589+ logger . appendKeys ( {
590+ duplicateKey : 'one'
591+ } ) ;
592+ logger . appendKeys ( {
593+ duplicateKey : 'two'
594+ } ) ;
595+
596+ // Assess
597+ expect ( logger ) . toEqual ( expect . objectContaining ( {
598+ persistentLogAttributes : {
599+ duplicateKey : 'two'
600+ }
601+ } ) ) ;
602+ } ) ;
526603 } ) ;
527604
528605 describe ( 'Method: createChild' , ( ) => {
0 commit comments