Skip to content

Commit fe4e73a

Browse files
committed
Add failing unit tests
1 parent c571a96 commit fe4e73a

File tree

1 file changed

+112
-14
lines changed

1 file changed

+112
-14
lines changed

packages/logger/tests/unit/Logger.test.ts

Lines changed: 112 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -439,26 +439,42 @@ describe('Class: Logger', () => {
439439

440440
describe('Method: addContext', () => {
441441

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+
442471
test('when called during a COLD START invocation, it populates the logger\'s PowertoolLogData object with coldstart set to true', () => {
443472

444473
// Prepare
445474
const logger = new Logger();
446475

447476
// 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);
462478

463479
// Assess
464480
expect(logger).toEqual({
@@ -492,6 +508,47 @@ describe('Class: Logger', () => {
492508
});
493509
});
494510

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+
});
495552
});
496553

497554
describe('Method: appendKeys', () => {
@@ -523,6 +580,47 @@ describe('Class: Logger', () => {
523580
},
524581
}));
525582
});
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+
});
526624
});
527625

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

0 commit comments

Comments
 (0)