From 30ea7b4de389eeb36860d32e595fbfe7a21e3716 Mon Sep 17 00:00:00 2001 From: Onni Koskinen Date: Sun, 6 Feb 2022 10:32:44 +0200 Subject: [PATCH 1/5] Add unit tests --- packages/logger/tests/unit/Logger.test.ts | 105 +++++++++++++++++++--- 1 file changed, 91 insertions(+), 14 deletions(-) diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index 8c3b04165d..06f10720e8 100644 --- a/packages/logger/tests/unit/Logger.test.ts +++ b/packages/logger/tests/unit/Logger.test.ts @@ -439,26 +439,28 @@ describe('Class: Logger', () => { describe('Method: addContext', () => { + const baseContext = { + callbackWaitsForEmptyEventLoop: true, + functionVersion: '$LATEST', + functionName: 'foo-bar-function-with-cold-start', + memoryLimitInMB: '128', + logGroupName: '/aws/lambda/foo-bar-function-with-cold-start', + logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456', + invokedFunctionArn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function-with-cold-start', + awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678', + getRemainingTimeInMillis: () => 1234, + done: () => console.log('Done!'), + fail: () => console.log('Failed!'), + succeed: () => console.log('Succeeded!'), + }; + test('when called during a COLD START invocation, it populates the logger\'s PowertoolLogData object with coldstart set to true', () => { // Prepare const logger = new Logger(); // Act - logger.addContext( { - callbackWaitsForEmptyEventLoop: true, - functionVersion: '$LATEST', - functionName: 'foo-bar-function-with-cold-start', - memoryLimitInMB: '128', - logGroupName: '/aws/lambda/foo-bar-function-with-cold-start', - logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456', - invokedFunctionArn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function-with-cold-start', - awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678', - getRemainingTimeInMillis: () => 1234, - done: () => console.log('Done!'), - fail: () => console.log('Failed!'), - succeed: () => console.log('Succeeded!'), - }); + logger.addContext({ ...baseContext }); // Assess expect(logger).toEqual({ @@ -492,6 +494,44 @@ describe('Class: Logger', () => { }); }); + test('user-provided context object is not mutated', () => { + + // Prepare + const logger = new Logger(); + const context1 = { ...baseContext, awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678' }; + const context2 = { ...baseContext, awsRequestId: 'd40c98a9-91c4-478c-a179-433c4b978289' }; + + // Act + logger.addContext(context1); + logger.addContext(context2); + + // Assess + expect(context1.awsRequestId).toEqual('c6af9ac6-7b61-11e6-9a41-93e812345678'); + expect(context2.awsRequestId).toEqual('d40c98a9-91c4-478c-a179-433c4b978289'); + }); + + test('when called multiple times, the newer values override earlier values', () => { + + // Prepare + const logger = new Logger(); + const context1 = { ...baseContext, awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678' }; + const context2 = { ...baseContext, awsRequestId: 'd40c98a9-91c4-478c-a179-433c4b978289' }; + + // Act + logger.addContext(context1); + logger.addContext(context2); + + // Assess + expect(logger).toEqual( + expect.objectContaining({ + powertoolLogData: expect.objectContaining({ + lambdaContext: expect.objectContaining({ + awsRequestId: context2.awsRequestId, + }) + }) + }) + ); + }); }); describe('Method: appendKeys', () => { @@ -523,6 +563,43 @@ describe('Class: Logger', () => { }, })); }); + + test('user-provided attribute object is not mutated', () => { + + // Prepare + const logger = new Logger(); + const attributes1 = { keyOne: 'abc' }; + const attributes2 = { keyTwo: 'def' }; + + // Act + logger.appendKeys(attributes1); + logger.appendKeys(attributes2); + + // Assess + expect(attributes1).toEqual({ keyOne: 'abc' }); + expect(attributes2).toEqual({ keyTwo: 'def' }); + }); + + test('when called multiple times, the newer values override earlier values', () => { + + // Prepare + const logger = new Logger(); + + // Act + logger.appendKeys({ + duplicateKey: 'one' + }); + logger.appendKeys({ + duplicateKey: 'two' + }); + + // Assess + expect(logger).toEqual(expect.objectContaining({ + persistentLogAttributes: { + duplicateKey: 'two' + } + })); + }); }); describe('Method: createChild', () => { From 2d8a85e07adf9c62a1284726f2f333e886fc6583 Mon Sep 17 00:00:00 2001 From: Onni Koskinen Date: Sun, 6 Feb 2022 10:32:59 +0200 Subject: [PATCH 2/5] Fix attribute merging priority order & mutation --- packages/logger/src/Logger.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index b018543a27..bba8ff3b51 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -174,7 +174,7 @@ class Logger implements ClassThatLogs { * @returns {void} */ public addPersistentLogAttributes(attributes?: LogAttributes): void { - this.persistentLogAttributes = merge(attributes, this.getPersistentLogAttributes()); + merge(this.persistentLogAttributes, attributes); } /** @@ -362,7 +362,7 @@ class Logger implements ClassThatLogs { */ private addToPowertoolLogData(...attributesArray: Array>): void { attributesArray.forEach((attributes: Partial) => { - this.powertoolLogData = merge(attributes, this.getPowertoolLogData()); + merge(this.powertoolLogData, attributes); }); } From d82c4ae76fded1947bc5ae2e028109524e1aefe5 Mon Sep 17 00:00:00 2001 From: Onni Koskinen Date: Wed, 9 Feb 2022 17:37:54 +0200 Subject: [PATCH 3/5] Update packages/logger/tests/unit/Logger.test.ts Co-authored-by: Andrea Amorosi --- packages/logger/tests/unit/Logger.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index 06f10720e8..4a1df4a6eb 100644 --- a/packages/logger/tests/unit/Logger.test.ts +++ b/packages/logger/tests/unit/Logger.test.ts @@ -564,7 +564,7 @@ describe('Class: Logger', () => { })); }); - test('user-provided attribute object is not mutated', () => { + test('when called with user-provided attribute objects, the objects are not mutated', () => { // Prepare const logger = new Logger(); From 952a09e69bb8c2d1c85bd46a6cc0969c662bd9ed Mon Sep 17 00:00:00 2001 From: Onni Koskinen Date: Wed, 9 Feb 2022 17:38:00 +0200 Subject: [PATCH 4/5] Update packages/logger/tests/unit/Logger.test.ts Co-authored-by: Andrea Amorosi --- packages/logger/tests/unit/Logger.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index 4a1df4a6eb..2921c04bc6 100644 --- a/packages/logger/tests/unit/Logger.test.ts +++ b/packages/logger/tests/unit/Logger.test.ts @@ -494,7 +494,7 @@ describe('Class: Logger', () => { }); }); - test('user-provided context object is not mutated', () => { + test('when called with a context object, the object is not mutated', () => { // Prepare const logger = new Logger(); From 9b90597c5a345c274423d869ed3297047da629f8 Mon Sep 17 00:00:00 2001 From: Onni Koskinen Date: Thu, 10 Feb 2022 22:26:14 +0200 Subject: [PATCH 5/5] Update packages/logger/tests/unit/Logger.test.ts Co-authored-by: Sara Gerion <47529391+saragerion@users.noreply.github.com> --- packages/logger/tests/unit/Logger.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index 2921c04bc6..ed6d543627 100644 --- a/packages/logger/tests/unit/Logger.test.ts +++ b/packages/logger/tests/unit/Logger.test.ts @@ -460,7 +460,7 @@ describe('Class: Logger', () => { const logger = new Logger(); // Act - logger.addContext({ ...baseContext }); + logger.addContext(baseContext); // Assess expect(logger).toEqual({