@@ -16,6 +16,10 @@ import { Console } from 'console';
16
16
17
17
const mockDate = new Date ( 1466424490000 ) ;
18
18
const dateSpy = jest . spyOn ( global , 'Date' ) . mockImplementation ( ( ) => mockDate ) ;
19
+ const getConsoleMethod = ( method : string ) : keyof Omit < ClassThatLogs , 'critical' > =>
20
+ method === 'critical' ?
21
+ 'error' :
22
+ method . toLowerCase ( ) as keyof Omit < ClassThatLogs , 'critical' > ;
19
23
20
24
describe ( 'Class: Logger' , ( ) => {
21
25
@@ -27,7 +31,8 @@ describe('Class: Logger', () => {
27
31
INFO : 12 ,
28
32
WARN : 16 ,
29
33
ERROR : 20 ,
30
- SILENT : 24 ,
34
+ CRITICAL : 24 ,
35
+ SILENT : 28 ,
31
36
} ;
32
37
33
38
beforeEach ( ( ) => {
@@ -40,6 +45,7 @@ describe('Class: Logger', () => {
40
45
[ 'info' , 'DOES' , true , 'DOES' , true , 'DOES NOT' , false , 'DOES NOT' , false ] ,
41
46
[ 'warn' , 'DOES' , true , 'DOES' , true , 'DOES' , true , 'DOES NOT' , false ] ,
42
47
[ 'error' , 'DOES' , true , 'DOES' , true , 'DOES' , true , 'DOES' , true ] ,
48
+ [ 'critical' , 'DOES' , true , 'DOES' , true , 'DOES' , true , 'DOES' , true ] ,
43
49
] ) (
44
50
'Method: %p' ,
45
51
(
@@ -54,17 +60,17 @@ describe('Class: Logger', () => {
54
60
errorPrints ,
55
61
) => {
56
62
57
- describe ( 'Feature: log level' , ( ) => {
63
+ const methodOfLogger = method as keyof ClassThatLogs ;
58
64
59
- const methodOfLogger = method as keyof ClassThatLogs ;
65
+ describe ( 'Feature: log level' , ( ) => {
60
66
61
67
test ( 'when the Logger\'s log level is DEBUG, it ' + debugAction + ' print to stdout' , ( ) => {
62
68
63
69
// Prepare
64
70
const logger : Logger = createLogger ( {
65
71
logLevel : 'DEBUG' ,
66
72
} ) ;
67
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
73
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( method ) ) . mockImplementation ( ) ;
68
74
69
75
// Act
70
76
logger [ methodOfLogger ] ( 'foo' ) ;
@@ -89,7 +95,7 @@ describe('Class: Logger', () => {
89
95
const logger : Logger = createLogger ( {
90
96
logLevel : 'INFO' ,
91
97
} ) ;
92
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
98
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
93
99
94
100
// Act
95
101
logger [ methodOfLogger ] ( 'foo' ) ;
@@ -114,7 +120,7 @@ describe('Class: Logger', () => {
114
120
const logger : Logger = createLogger ( {
115
121
logLevel : 'WARN' ,
116
122
} ) ;
117
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
123
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
118
124
119
125
// Act
120
126
logger [ methodOfLogger ] ( 'foo' ) ;
@@ -139,7 +145,7 @@ describe('Class: Logger', () => {
139
145
const logger : Logger = createLogger ( {
140
146
logLevel : 'ERROR' ,
141
147
} ) ;
142
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
148
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
143
149
144
150
// Act
145
151
logger [ methodOfLogger ] ( 'foo' ) ;
@@ -164,7 +170,7 @@ describe('Class: Logger', () => {
164
170
const logger : Logger = createLogger ( {
165
171
logLevel : 'SILENT' ,
166
172
} ) ;
167
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
173
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
168
174
169
175
// Act
170
176
logger [ methodOfLogger ] ( 'foo' ) ;
@@ -178,7 +184,7 @@ describe('Class: Logger', () => {
178
184
// Prepare
179
185
process . env . LOG_LEVEL = methodOfLogger . toUpperCase ( ) ;
180
186
const logger = new Logger ( ) ;
181
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
187
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
182
188
183
189
// Act
184
190
logger [ methodOfLogger ] ( 'foo' ) ;
@@ -197,16 +203,14 @@ describe('Class: Logger', () => {
197
203
198
204
describe ( 'Feature: sample rate' , ( ) => {
199
205
200
- const methodOfLogger = method as keyof ClassThatLogs ;
201
-
202
206
test ( 'when the Logger\'s log level is higher and the current Lambda invocation IS NOT sampled for logging, it DOES NOT print to stdout' , ( ) => {
203
207
204
208
// Prepare
205
209
const logger : Logger = createLogger ( {
206
210
logLevel : 'SILENT' ,
207
211
sampleRateValue : 0 ,
208
212
} ) ;
209
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
213
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
210
214
211
215
// Act
212
216
if ( logger [ methodOfLogger ] ) {
@@ -224,7 +228,7 @@ describe('Class: Logger', () => {
224
228
logLevel : 'SILENT' ,
225
229
sampleRateValue : 1 ,
226
230
} ) ;
227
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
231
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
228
232
229
233
// Act
230
234
if ( logger [ methodOfLogger ] ) {
@@ -247,13 +251,11 @@ describe('Class: Logger', () => {
247
251
248
252
describe ( 'Feature: inject context' , ( ) => {
249
253
250
- const methodOfLogger = method as keyof ClassThatLogs ;
251
-
252
254
test ( 'when the Lambda context is not captured and a string is passed as log message, it should print a valid ' + method . toUpperCase ( ) + ' log' , ( ) => {
253
255
254
256
// Prepare
255
257
const logger : Logger = createLogger ( ) ;
256
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
258
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
257
259
258
260
// Act
259
261
if ( logger [ methodOfLogger ] ) {
@@ -279,7 +281,7 @@ describe('Class: Logger', () => {
279
281
logLevel : 'DEBUG' ,
280
282
} ) ;
281
283
logger . addContext ( context ) ;
282
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
284
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
283
285
284
286
// Act
285
287
if ( logger [ methodOfLogger ] ) {
@@ -307,15 +309,13 @@ describe('Class: Logger', () => {
307
309
308
310
describe ( 'Feature: ephemeral log attributes' , ( ) => {
309
311
310
- const methodOfLogger = method as keyof ClassThatLogs ;
311
-
312
312
test ( 'when added, they should appear in that log item only' , ( ) => {
313
313
314
314
// Prepare
315
315
const logger : Logger = createLogger ( {
316
316
logLevel : 'DEBUG' ,
317
317
} ) ;
318
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
318
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
319
319
320
320
interface NestedObject { bool : boolean ; str : string ; num : number ; err : Error }
321
321
interface ArbitraryObject < TNested > { value : 'CUSTOM' | 'USER_DEFINED' ; nested : TNested }
@@ -444,8 +444,6 @@ describe('Class: Logger', () => {
444
444
445
445
describe ( 'Feature: persistent log attributes' , ( ) => {
446
446
447
- const methodOfLogger = method as keyof ClassThatLogs ;
448
-
449
447
test ( 'when persistent log attributes are added to the Logger instance, they should appear in all logs printed by the instance' , ( ) => {
450
448
451
449
// Prepare
@@ -456,7 +454,7 @@ describe('Class: Logger', () => {
456
454
aws_region : 'eu-west-1' ,
457
455
} ,
458
456
} ) ;
459
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
457
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
460
458
461
459
// Act
462
460
if ( logger [ methodOfLogger ] ) {
@@ -481,15 +479,13 @@ describe('Class: Logger', () => {
481
479
482
480
describe ( 'Feature: X-Ray Trace ID injection' , ( ) => {
483
481
484
- const methodOfLogger = method as keyof ClassThatLogs ;
485
-
486
482
test ( 'when the `_X_AMZN_TRACE_ID` environment variable is set it parses it correctly and adds the Trace ID to the log' , ( ) => {
487
483
488
484
// Prepare
489
485
const logger : Logger = createLogger ( {
490
486
logLevel : 'DEBUG' ,
491
487
} ) ;
492
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
488
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
493
489
494
490
// Act
495
491
if ( logger [ methodOfLogger ] ) {
@@ -515,7 +511,7 @@ describe('Class: Logger', () => {
515
511
const logger : Logger = createLogger ( {
516
512
logLevel : 'DEBUG' ,
517
513
} ) ;
518
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
514
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
519
515
520
516
// Act
521
517
if ( logger [ methodOfLogger ] ) {
@@ -537,15 +533,13 @@ describe('Class: Logger', () => {
537
533
538
534
describe ( 'Feature: handle safely unexpected errors' , ( ) => {
539
535
540
- const methodOfLogger = method as keyof ClassThatLogs ;
541
-
542
536
test ( 'when a logged item references itself, the logger ignores the keys that cause a circular reference' , ( ) => {
543
537
544
538
// Prepare
545
539
const logger : Logger = createLogger ( {
546
540
logLevel : 'DEBUG' ,
547
541
} ) ;
548
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
542
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
549
543
const circularObject = {
550
544
foo : 'bar' ,
551
545
self : { } ,
@@ -581,7 +575,7 @@ describe('Class: Logger', () => {
581
575
582
576
// Prepare
583
577
const logger = new Logger ( ) ;
584
- jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
578
+ jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
585
579
const message = `This is an ${ methodOfLogger } log with BigInt value` ;
586
580
const logItem = { value : BigInt ( 42 ) } ;
587
581
const errorMessage = 'Do not know how to serialize a BigInt' ;
@@ -595,7 +589,7 @@ describe('Class: Logger', () => {
595
589
596
590
// Prepare
597
591
const logger = new Logger ( ) ;
598
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
592
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
599
593
const message = `This is an ${ methodOfLogger } log with BigInt value` ;
600
594
const logItem = { value : BigInt ( 42 ) } ;
601
595
@@ -619,7 +613,7 @@ describe('Class: Logger', () => {
619
613
620
614
// Prepare
621
615
const logger = new Logger ( ) ;
622
- const consoleSpy = jest . spyOn ( logger [ 'console' ] , methodOfLogger ) . mockImplementation ( ) ;
616
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , getConsoleMethod ( methodOfLogger ) ) . mockImplementation ( ) ;
623
617
const message = `This is an ${ methodOfLogger } log with empty, null, and undefined values` ;
624
618
const logItem = { value : 42 , emptyValue : '' , undefinedValue : undefined , nullValue : null } ;
625
619
@@ -1050,6 +1044,7 @@ describe('Class: Logger', () => {
1050
1044
biz : 'baz'
1051
1045
}
1052
1046
} ) ;
1047
+ jest . spyOn ( logger [ 'console' ] , 'debug' ) . mockImplementation ( ) ;
1053
1048
class LambdaFunction implements LambdaInterface {
1054
1049
1055
1050
@logger . injectLambdaContext ( { clearState : true } )
@@ -1091,6 +1086,7 @@ describe('Class: Logger', () => {
1091
1086
biz : 'baz'
1092
1087
}
1093
1088
} ) ;
1089
+ jest . spyOn ( logger [ 'console' ] , 'debug' ) . mockImplementation ( ) ;
1094
1090
class LambdaFunction implements LambdaInterface {
1095
1091
1096
1092
@logger . injectLambdaContext ( { clearState : true } )
0 commit comments