Skip to content

Commit fd5862a

Browse files
committed
test(logger): add test case for loglevel trace (aws-powertools#1589)
1 parent 4fff41a commit fd5862a

File tree

1 file changed

+100
-4
lines changed

1 file changed

+100
-4
lines changed

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

+100-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ describe('Class: Logger', () => {
4646
bar: 'baz',
4747
};
4848
const logLevelThresholds: LogLevelThresholds = {
49+
TRACE: 6,
4950
DEBUG: 8,
5051
INFO: 12,
5152
WARN: 16,
@@ -567,25 +568,90 @@ describe('Class: Logger', () => {
567568
});
568569

569570
describe.each([
571+
[
572+
'trace',
573+
'DOES',
574+
true,
575+
'DOES NOT',
576+
false,
577+
'DOES NOT',
578+
false,
579+
'DOES NOT',
580+
false,
581+
'DOES NOT',
582+
false,
583+
],
570584
[
571585
'debug',
572586
'DOES',
573587
true,
588+
'DOES',
589+
true,
590+
'DOES NOT',
591+
false,
592+
'DOES NOT',
593+
false,
594+
'DOES NOT',
595+
false,
596+
],
597+
[
598+
'info',
599+
'DOES',
600+
true,
601+
'DOES',
602+
true,
603+
'DOES',
604+
true,
574605
'DOES NOT',
575606
false,
576607
'DOES NOT',
577608
false,
609+
],
610+
[
611+
'warn',
612+
'DOES',
613+
true,
614+
'DOES',
615+
true,
616+
'DOES',
617+
true,
618+
'DOES',
619+
true,
578620
'DOES NOT',
579621
false,
580622
],
581-
['info', 'DOES', true, 'DOES', true, 'DOES NOT', false, 'DOES NOT', false],
582-
['warn', 'DOES', true, 'DOES', true, 'DOES', true, 'DOES NOT', false],
583-
['error', 'DOES', true, 'DOES', true, 'DOES', true, 'DOES', true],
584-
['critical', 'DOES', true, 'DOES', true, 'DOES', true, 'DOES', true],
623+
[
624+
'error',
625+
'DOES',
626+
true,
627+
'DOES',
628+
true,
629+
'DOES',
630+
true,
631+
'DOES',
632+
true,
633+
'DOES',
634+
true,
635+
],
636+
[
637+
'critical',
638+
'DOES',
639+
true,
640+
'DOES',
641+
true,
642+
'DOES',
643+
true,
644+
'DOES',
645+
true,
646+
'DOES',
647+
true,
648+
],
585649
])(
586650
'Method:',
587651
(
588652
method: string,
653+
traceAction,
654+
tracePrints,
589655
debugAction,
590656
debugPrints,
591657
infoAction,
@@ -598,6 +664,36 @@ describe('Class: Logger', () => {
598664
const methodOfLogger = method as keyof LogFunction;
599665

600666
describe('Feature: log level', () => {
667+
test(`when the level is TRACE, it ${traceAction} print to stdout`, () => {
668+
// Prepare
669+
const logger = new Logger({
670+
logLevel: LogLevel.TRACE,
671+
});
672+
const consoleSpy = jest.spyOn(
673+
// biome-ignore lint/complexity/useLiteralKeys: This needs to be accessed with literal key for testing
674+
logger['console'],
675+
getConsoleMethod(method)
676+
);
677+
// Act
678+
logger[methodOfLogger]('foo');
679+
680+
// Assess
681+
expect(consoleSpy).toBeCalledTimes(tracePrints ? 1 : 0);
682+
if (tracePrints) {
683+
expect(consoleSpy).toHaveBeenNthCalledWith(
684+
1,
685+
JSON.stringify({
686+
level: methodOfLogger.toUpperCase(),
687+
message: 'foo',
688+
sampling_rate: 0,
689+
service: 'hello-world',
690+
timestamp: '2016-06-20T12:08:10.000Z',
691+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
692+
})
693+
);
694+
}
695+
});
696+
601697
test(`when the level is DEBUG, it ${debugAction} print to stdout`, () => {
602698
// Prepare
603699
const logger = new Logger({

0 commit comments

Comments
 (0)