Skip to content

Commit 5c7ebbe

Browse files
committed
Add failing unit tests
1 parent c571a96 commit 5c7ebbe

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Diff for: packages/logger/tests/unit/Logger.test.ts

+79
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,44 @@ describe('Class: Logger', () => {
492492
});
493493
});
494494

495+
test('when called multiple times, it obeys the newest context values', () => {
496+
497+
// Prepare
498+
const logger = new Logger();
499+
const context1 = {
500+
callbackWaitsForEmptyEventLoop: true,
501+
functionVersion: '$LATEST',
502+
functionName: 'foo-bar-function-with-cold-start',
503+
memoryLimitInMB: '128',
504+
logGroupName: '/aws/lambda/foo-bar-function-with-cold-start',
505+
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
506+
invokedFunctionArn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function-with-cold-start',
507+
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
508+
getRemainingTimeInMillis: () => 1234,
509+
done: () => console.log('Done!'),
510+
fail: () => console.log('Failed!'),
511+
succeed: () => console.log('Succeeded!'),
512+
};
513+
const context2 = {
514+
...context1,
515+
awsRequestId: 'd40c98a9-91c4-478c-a179-433c4b978289',
516+
};
517+
518+
// Act
519+
logger.addContext(context1);
520+
logger.addContext(context2);
521+
522+
// Assess
523+
expect(logger).toEqual(
524+
expect.objectContaining({
525+
powertoolLogData: expect.objectContaining({
526+
lambdaContext: expect.objectContaining({
527+
awsRequestId: context2.awsRequestId,
528+
})
529+
})
530+
})
531+
);
532+
});
495533
});
496534

497535
describe('Method: appendKeys', () => {
@@ -523,6 +561,47 @@ describe('Class: Logger', () => {
523561
},
524562
}));
525563
});
564+
565+
test('user-provided attribute object is not mutated', () => {
566+
567+
// Prepare
568+
const logger = new Logger();
569+
const attributes1 = { keyOne: 'abc' };
570+
const attributes2 = { keyTwo: 'def' };
571+
572+
// Act
573+
logger.appendKeys(attributes1);
574+
logger.appendKeys(attributes2);
575+
576+
// Assess
577+
expect(attributes1).toEqual({ keyOne: 'abc' });
578+
expect(attributes2).toEqual({ keyTwo: 'def' });
579+
});
580+
581+
test('when called multiple times with same key, the last value overrides earlier values', () => {
582+
583+
// Prepare
584+
const logger = new Logger();
585+
586+
// Act
587+
logger.appendKeys({
588+
keyOne: 'abc',
589+
duplicateKey: 'one'
590+
});
591+
logger.appendKeys({
592+
keyTwo: 'def',
593+
duplicateKey: 'two'
594+
});
595+
596+
// Assess
597+
expect(logger).toEqual(expect.objectContaining({
598+
persistentLogAttributes: {
599+
keyOne: 'abc',
600+
keyTwo: 'def',
601+
duplicateKey: 'two'
602+
}
603+
}));
604+
});
526605
});
527606

528607
describe('Method: createChild', () => {

0 commit comments

Comments
 (0)