@@ -14,24 +14,16 @@ import { EnvironmentVariablesService } from '../../src/config';
14
14
import { PowertoolLogFormatter } from '../../src/formatter' ;
15
15
import { ClassThatLogs } from '../../src/types' ;
16
16
import { Context } from 'aws-lambda' ;
17
+ import { Console } from 'console' ;
17
18
18
19
const mockDate = new Date ( 1466424490000 ) ;
19
20
const dateSpy = jest . spyOn ( global , 'Date' ) . mockImplementation ( ( ) => mockDate as unknown as string ) ;
20
-
21
- const consoleSpy = {
22
- 'debug' : jest . spyOn ( console , 'debug' ) . mockImplementation ( ) ,
23
- 'info' : jest . spyOn ( console , 'info' ) . mockImplementation ( ) ,
24
- 'warn' : jest . spyOn ( console , 'warn' ) . mockImplementation ( ) ,
25
- 'error' : jest . spyOn ( console , 'error' ) . mockImplementation ( ) ,
26
- } ;
21
+ const stdoutSpy = jest . spyOn ( process . stdout , 'write' ) . mockImplementation ( ( ) => true ) ;
27
22
28
23
describe ( 'Class: Logger' , ( ) => {
29
24
30
25
beforeEach ( ( ) => {
31
- consoleSpy [ 'debug' ] . mockClear ( ) ;
32
- consoleSpy [ 'info' ] . mockClear ( ) ;
33
- consoleSpy [ 'warn' ] . mockClear ( ) ;
34
- consoleSpy [ 'error' ] . mockClear ( ) ;
26
+ stdoutSpy . mockClear ( ) ;
35
27
dateSpy . mockClear ( ) ;
36
28
} ) ;
37
29
@@ -70,9 +62,9 @@ describe('Class: Logger', () => {
70
62
}
71
63
72
64
// Assess
73
- expect ( console [ methodOfLogger ] ) . toBeCalledTimes ( debugPrints ? 1 : 0 ) ;
65
+ expect ( stdoutSpy ) . toBeCalledTimes ( debugPrints ? 1 : 0 ) ;
74
66
if ( debugPrints ) {
75
- expect ( console [ methodOfLogger ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
67
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
76
68
level : methodOfLogger . toUpperCase ( ) ,
77
69
message : 'foo' ,
78
70
service : 'hello-world' ,
@@ -96,9 +88,9 @@ describe('Class: Logger', () => {
96
88
}
97
89
98
90
// Assess
99
- expect ( console [ methodOfLogger ] ) . toBeCalledTimes ( infoPrints ? 1 : 0 ) ;
91
+ expect ( stdoutSpy ) . toBeCalledTimes ( infoPrints ? 1 : 0 ) ;
100
92
if ( infoPrints ) {
101
- expect ( console [ methodOfLogger ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
93
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
102
94
level : methodOfLogger . toUpperCase ( ) ,
103
95
message : 'foo' ,
104
96
service : 'hello-world' ,
@@ -122,9 +114,9 @@ describe('Class: Logger', () => {
122
114
}
123
115
124
116
// Assess
125
- expect ( console [ methodOfLogger ] ) . toBeCalledTimes ( warnPrints ? 1 : 0 ) ;
117
+ expect ( stdoutSpy ) . toBeCalledTimes ( warnPrints ? 1 : 0 ) ;
126
118
if ( warnPrints ) {
127
- expect ( console [ methodOfLogger ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
119
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
128
120
level : methodOfLogger . toUpperCase ( ) ,
129
121
message : 'foo' ,
130
122
service : 'hello-world' ,
@@ -148,9 +140,9 @@ describe('Class: Logger', () => {
148
140
}
149
141
150
142
// Assess
151
- expect ( console [ methodOfLogger ] ) . toBeCalledTimes ( errorPrints ? 1 : 0 ) ;
143
+ expect ( stdoutSpy ) . toBeCalledTimes ( errorPrints ? 1 : 0 ) ;
152
144
if ( errorPrints ) {
153
- expect ( console [ methodOfLogger ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
145
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
154
146
level : methodOfLogger . toUpperCase ( ) ,
155
147
message : 'foo' ,
156
148
service : 'hello-world' ,
@@ -181,7 +173,7 @@ describe('Class: Logger', () => {
181
173
}
182
174
183
175
// Assess
184
- expect ( console [ methodOfLogger ] ) . toBeCalledTimes ( method === 'error' ? 1 : 0 ) ;
176
+ expect ( stdoutSpy ) . toBeCalledTimes ( method === 'error' ? 1 : 0 ) ;
185
177
} ) ;
186
178
187
179
test ( 'when the Logger\'s log level is higher and the current Lambda invocation IS sampled for logging, it DOES print to stdout' , ( ) => {
@@ -198,8 +190,8 @@ describe('Class: Logger', () => {
198
190
}
199
191
200
192
// Assess
201
- expect ( console [ methodOfLogger ] ) . toBeCalledTimes ( 1 ) ;
202
- expect ( console [ methodOfLogger ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
193
+ expect ( stdoutSpy ) . toBeCalledTimes ( 1 ) ;
194
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
203
195
level : method . toUpperCase ( ) ,
204
196
message : 'foo' ,
205
197
sampling_rate : 1 ,
@@ -226,8 +218,8 @@ describe('Class: Logger', () => {
226
218
}
227
219
228
220
// Assess
229
- expect ( console [ methodOfLogger ] ) . toBeCalledTimes ( 1 ) ;
230
- expect ( console [ methodOfLogger ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
221
+ expect ( stdoutSpy ) . toBeCalledTimes ( 1 ) ;
222
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
231
223
level : method . toUpperCase ( ) ,
232
224
message : 'foo' ,
233
225
service : 'hello-world' ,
@@ -250,8 +242,8 @@ describe('Class: Logger', () => {
250
242
}
251
243
252
244
// Assess
253
- expect ( console [ methodOfLogger ] ) . toBeCalledTimes ( 1 ) ;
254
- expect ( console [ methodOfLogger ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
245
+ expect ( stdoutSpy ) . toBeCalledTimes ( 1 ) ;
246
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
255
247
cold_start : true ,
256
248
function_arn : 'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
257
249
function_memory_size : 128 ,
@@ -292,22 +284,22 @@ describe('Class: Logger', () => {
292
284
}
293
285
294
286
// Assess
295
- expect ( console [ methodOfConsole ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
287
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
296
288
level : method . toUpperCase ( ) ,
297
289
message : 'A log item without extra parameters' ,
298
290
service : 'hello-world' ,
299
291
timestamp : '2016-06-20T12:08:10.000Z' ,
300
292
xray_trace_id : 'abcdef123456abcdef123456abcdef123456' ,
301
293
} ) ) ;
302
- expect ( console [ methodOfConsole ] ) . toHaveBeenNthCalledWith ( 2 , JSON . stringify ( {
294
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 2 , JSON . stringify ( {
303
295
level : method . toUpperCase ( ) ,
304
296
message : 'A log item with a string as first parameter, and an object as second parameter' ,
305
297
service : 'hello-world' ,
306
298
timestamp : '2016-06-20T12:08:10.000Z' ,
307
299
xray_trace_id : 'abcdef123456abcdef123456abcdef123456' ,
308
300
extra : 'parameter' ,
309
301
} ) ) ;
310
- expect ( console [ methodOfConsole ] ) . toHaveBeenNthCalledWith ( 3 , JSON . stringify ( {
302
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 3 , JSON . stringify ( {
311
303
level : method . toUpperCase ( ) ,
312
304
message : 'A log item with a string as first parameter, and objects as other parameters' ,
313
305
service : 'hello-world' ,
@@ -316,15 +308,15 @@ describe('Class: Logger', () => {
316
308
parameterOne : 'foo' ,
317
309
parameterTwo : 'bar' ,
318
310
} ) ) ;
319
- expect ( console [ methodOfConsole ] ) . toHaveBeenNthCalledWith ( 4 , JSON . stringify ( {
311
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 4 , JSON . stringify ( {
320
312
level : method . toUpperCase ( ) ,
321
313
message : 'A log item with an object as first parameters' ,
322
314
service : 'hello-world' ,
323
315
timestamp : '2016-06-20T12:08:10.000Z' ,
324
316
xray_trace_id : 'abcdef123456abcdef123456abcdef123456' ,
325
317
extra : 'parameter' ,
326
318
} ) ) ;
327
- const parameterCallNumber5 = JSON . parse ( consoleSpy [ methodOfConsole ] . mock . calls [ 4 ] [ 0 ] ) ;
319
+ const parameterCallNumber5 = JSON . parse ( stdoutSpy . mock . calls [ 4 ] [ 0 ] as string ) ;
328
320
expect ( parameterCallNumber5 ) . toEqual ( expect . objectContaining ( {
329
321
level : method . toUpperCase ( ) ,
330
322
message : 'A log item with a string as first parameter, and an error as second parameter' ,
@@ -338,7 +330,7 @@ describe('Class: Logger', () => {
338
330
stack : expect . stringMatching ( / L o g g e r .t e s t .t s : [ 0 - 9 ] + : [ 0 - 9 ] + / ) ,
339
331
} ,
340
332
} ) ) ;
341
- const parameterCallNumber6 = JSON . parse ( consoleSpy [ methodOfConsole ] . mock . calls [ 5 ] [ 0 ] ) ;
333
+ const parameterCallNumber6 = JSON . parse ( stdoutSpy . mock . calls [ 5 ] [ 0 ] as string ) ;
342
334
expect ( parameterCallNumber6 ) . toEqual ( expect . objectContaining ( {
343
335
level : method . toUpperCase ( ) ,
344
336
message : 'A log item with a string as first parameter, and an error with custom key as second parameter' ,
@@ -352,7 +344,7 @@ describe('Class: Logger', () => {
352
344
stack : expect . stringMatching ( / L o g g e r .t e s t .t s : [ 0 - 9 ] + : [ 0 - 9 ] + / ) ,
353
345
} ,
354
346
} ) ) ;
355
- expect ( console [ methodOfConsole ] ) . toHaveBeenNthCalledWith ( 7 , JSON . stringify ( {
347
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 7 , JSON . stringify ( {
356
348
level : method . toUpperCase ( ) ,
357
349
message : 'A log item with a string as first parameter, and a string as second parameter' ,
358
350
service : 'hello-world' ,
@@ -385,8 +377,8 @@ describe('Class: Logger', () => {
385
377
}
386
378
387
379
// Assess
388
- expect ( console [ methodOfConsole ] ) . toBeCalledTimes ( 1 ) ;
389
- expect ( console [ methodOfConsole ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
380
+ expect ( stdoutSpy ) . toBeCalledTimes ( 1 ) ;
381
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
390
382
level : method . toUpperCase ( ) ,
391
383
message : 'foo' ,
392
384
service : 'hello-world' ,
@@ -428,7 +420,7 @@ describe('Class: Logger', () => {
428
420
429
421
// Assess
430
422
expect ( result ) . toBe ( 'All good!' ) ;
431
- expect ( console [ methodOfConsole ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
423
+ expect ( stdoutSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
432
424
level : method . toUpperCase ( ) ,
433
425
message : 'A log with a circular reference' ,
434
426
service : 'hello-world' ,
@@ -471,6 +463,7 @@ describe('Class: Logger', () => {
471
463
472
464
// Assess
473
465
expect ( logger ) . toEqual ( {
466
+ console : expect . any ( Console ) ,
474
467
coldStart : false , // This is now false because the `coldStart` attribute has been already accessed once by the `addContext` method
475
468
customConfigService : undefined ,
476
469
envVarsService : expect . any ( EnvironmentVariablesService ) ,
@@ -723,15 +716,17 @@ describe('Class: Logger', () => {
723
716
// Assess
724
717
725
718
expect ( actualResult ) . toEqual ( expectedReturnValue ) ;
726
- expect ( console [ 'info' ] ) . toBeCalledTimes ( 2 ) ;
727
- expect ( console [ 'info' ] ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
719
+ expect ( stdoutSpy ) . toBeCalledTimes ( 2 ) ;
720
+ const actualCall1 = stdoutSpy . mock . calls [ 0 ] [ 0 ] ;
721
+ expect ( actualCall1 ) . toEqual ( JSON . stringify ( {
728
722
level : 'INFO' ,
729
723
message : 'An INFO log without context!' ,
730
724
service : 'hello-world' ,
731
725
timestamp : '2016-06-20T12:08:10.000Z' ,
732
726
xray_trace_id : 'abcdef123456abcdef123456abcdef123456' ,
733
727
} ) ) ;
734
- expect ( console [ 'info' ] ) . toHaveBeenNthCalledWith ( 2 , JSON . stringify ( {
728
+ const actualCall2 = stdoutSpy . mock . calls [ 0 ] [ 1 ] ;
729
+ expect ( actualCall2 ) . toEqual ( JSON . stringify ( {
735
730
cold_start : true ,
736
731
function_arn : 'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
737
732
function_memory_size : 128 ,
@@ -804,6 +799,7 @@ describe('Class: Logger', () => {
804
799
expect ( parentLogger === childLoggerWithErrorLogLevel ) . toBe ( false ) ;
805
800
806
801
expect ( parentLogger ) . toEqual ( {
802
+ console : expect . any ( Console ) ,
807
803
coldStart : true ,
808
804
customConfigService : undefined ,
809
805
envVarsService : expect . any ( EnvironmentVariablesService ) ,
@@ -827,6 +823,7 @@ describe('Class: Logger', () => {
827
823
} ) ;
828
824
829
825
expect ( childLoggerWithPermanentAttributes ) . toEqual ( {
826
+ console : expect . any ( Console ) ,
830
827
coldStart : true ,
831
828
customConfigService : undefined ,
832
829
envVarsService : expect . any ( EnvironmentVariablesService ) ,
@@ -852,6 +849,7 @@ describe('Class: Logger', () => {
852
849
} ) ;
853
850
854
851
expect ( childLoggerWithSampleRateEnabled ) . toEqual ( {
852
+ console : expect . any ( Console ) ,
855
853
coldStart : true ,
856
854
customConfigService : undefined ,
857
855
envVarsService : expect . any ( EnvironmentVariablesService ) ,
@@ -875,6 +873,7 @@ describe('Class: Logger', () => {
875
873
} ) ;
876
874
877
875
expect ( childLoggerWithErrorLogLevel ) . toEqual ( {
876
+ console : expect . any ( Console ) ,
878
877
coldStart : true ,
879
878
customConfigService : undefined ,
880
879
envVarsService : expect . any ( EnvironmentVariablesService ) ,
0 commit comments