@@ -8,22 +8,34 @@ describe('ContextLogger', () => {
8
8
const spyWarn = jest . fn ( ) ;
9
9
const spyError = jest . fn ( ) ;
10
10
const MODULE_NAME = 'TestModule' ;
11
- const contextLogger = new ContextLogger ( MODULE_NAME ) ;
12
11
const CONTEXT = { someContextField : 'someContextValue' } ;
13
12
14
- jest . spyOn ( ContextStore , 'getContext' ) . mockReturnValue ( CONTEXT ) ;
15
- const mockLogger = {
16
- log : spyLog ,
17
- debug : spyDebug ,
18
- info : spyInfo ,
19
- warn : spyWarn ,
20
- error : spyError ,
21
- } ;
13
+ let contextLogger : ContextLogger ;
14
+ let mockLogger : any ;
22
15
23
- ContextLogger . init ( mockLogger as any ) ;
16
+ beforeEach ( ( ) => {
17
+ jest . clearAllMocks ( ) ;
18
+
19
+ jest . spyOn ( ContextStore , 'getContext' ) . mockReturnValue ( CONTEXT ) ;
20
+
21
+ mockLogger = {
22
+ log : spyLog ,
23
+ debug : spyDebug ,
24
+ info : spyInfo ,
25
+ warn : spyWarn ,
26
+ error : spyError ,
27
+ } ;
28
+
29
+ // Reset the internal logger for each test to ensure clean state
30
+ ( ContextLogger as any ) . internalLogger = null ;
31
+ ContextLogger . init ( mockLogger ) ;
32
+
33
+ contextLogger = new ContextLogger ( MODULE_NAME ) ;
34
+ } ) ;
24
35
25
36
afterEach ( ( ) => {
26
37
jest . clearAllMocks ( ) ;
38
+ jest . restoreAllMocks ( ) ;
27
39
} ) ;
28
40
29
41
describe ( 'log, debug, warn methods' , ( ) => {
@@ -47,6 +59,20 @@ describe('ContextLogger', () => {
47
59
48
60
expect ( mockLogger . log ) . toHaveBeenCalledWith ( CONTEXT , message , MODULE_NAME ) ;
49
61
} ) ;
62
+
63
+ it ( 'should call log method when info is called' , ( ) => {
64
+ const message = 'Test message' ;
65
+ const bindings = { someBinding : 'value' } ;
66
+
67
+ contextLogger . info ( message , bindings ) ;
68
+
69
+ // Since info method calls log internally
70
+ expect ( mockLogger . log ) . toHaveBeenCalledWith (
71
+ { ...bindings , ...CONTEXT } ,
72
+ message ,
73
+ MODULE_NAME
74
+ ) ;
75
+ } ) ;
50
76
} ) ;
51
77
52
78
describe ( 'error method' , ( ) => {
@@ -147,7 +173,8 @@ describe('ContextLogger', () => {
147
173
148
174
afterEach ( ( ) => {
149
175
fallbackLoggerSpy . mockRestore ( ) ;
150
- ContextLogger . init ( mockLogger as any ) ;
176
+ // Restore the internal logger
177
+ ContextLogger . init ( mockLogger ) ;
151
178
} ) ;
152
179
153
180
it ( 'should handle bootstrap component logs with string bindings' , ( ) => {
@@ -187,7 +214,7 @@ describe('ContextLogger', () => {
187
214
const logger = new ContextLogger ( MODULE_NAME ) ;
188
215
logger . info ( 'test message' , { key : 'value' } ) ;
189
216
190
- expect ( spyInfo ) . toHaveBeenCalledWith (
217
+ expect ( spyLog ) . toHaveBeenCalledWith (
191
218
expect . objectContaining ( {
192
219
key : 'value' ,
193
220
} ) ,
@@ -203,7 +230,7 @@ describe('ContextLogger', () => {
203
230
jest . spyOn ( ContextStore , 'getContext' ) . mockReturnValue ( contextData ) ;
204
231
logger . info ( 'test message' ) ;
205
232
206
- expect ( spyInfo ) . toHaveBeenCalledWith (
233
+ expect ( spyLog ) . toHaveBeenCalledWith (
207
234
expect . objectContaining ( {
208
235
user : 'john' ,
209
236
} ) ,
@@ -214,15 +241,20 @@ describe('ContextLogger', () => {
214
241
} ) ;
215
242
216
243
describe ( 'with partial grouping' , ( ) => {
244
+ beforeEach ( ( ) => {
245
+ // Reset the internal logger for each test to ensure clean state
246
+ ( ContextLogger as any ) . internalLogger = null ;
247
+ } ) ;
248
+
217
249
it ( 'should group only bindings when bindingsKey provided' , ( ) => {
218
250
const logger = new ContextLogger ( MODULE_NAME ) ;
219
- ContextLogger . init ( mockLogger as any , { groupFields : { bindingsKey : 'params' } } ) ;
251
+ ContextLogger . init ( mockLogger , { groupFields : { bindingsKey : 'params' } } ) ;
220
252
const contextData = { user : 'john' } ;
221
253
222
254
jest . spyOn ( ContextStore , 'getContext' ) . mockReturnValue ( contextData ) ;
223
255
logger . info ( 'test message' , { key : 'value' } ) ;
224
256
225
- expect ( spyInfo ) . toHaveBeenCalledWith (
257
+ expect ( spyLog ) . toHaveBeenCalledWith (
226
258
expect . objectContaining ( {
227
259
params : { key : 'value' } ,
228
260
user : 'john' ,
@@ -234,13 +266,13 @@ describe('ContextLogger', () => {
234
266
235
267
it ( 'should not group bindings when bindingsKey provided but bindings object is empty' , ( ) => {
236
268
const logger = new ContextLogger ( MODULE_NAME ) ;
237
- ContextLogger . init ( mockLogger as any , { groupFields : { bindingsKey : 'params' } } ) ;
269
+ ContextLogger . init ( mockLogger , { groupFields : { bindingsKey : 'params' } } ) ;
238
270
const contextData = { user : 'john' } ;
239
271
240
272
jest . spyOn ( ContextStore , 'getContext' ) . mockReturnValue ( contextData ) ;
241
273
logger . info ( 'test message' ) ;
242
274
243
- expect ( spyInfo ) . toHaveBeenCalledWith (
275
+ expect ( spyLog ) . toHaveBeenCalledWith (
244
276
{ user : 'john' , } ,
245
277
'test message' ,
246
278
MODULE_NAME
@@ -249,13 +281,13 @@ describe('ContextLogger', () => {
249
281
250
282
it ( 'should group only context when contextKey provided' , ( ) => {
251
283
const logger = new ContextLogger ( MODULE_NAME ) ;
252
- ContextLogger . init ( mockLogger as any , { groupFields : { contextKey : 'metadata' } } ) ;
284
+ ContextLogger . init ( mockLogger , { groupFields : { contextKey : 'metadata' } } ) ;
253
285
const contextData = { user : 'john' } ;
254
286
255
287
jest . spyOn ( ContextStore , 'getContext' ) . mockReturnValue ( contextData ) ;
256
288
logger . info ( 'test message' , { key : 'value' } ) ;
257
289
258
- expect ( spyInfo ) . toHaveBeenCalledWith (
290
+ expect ( spyLog ) . toHaveBeenCalledWith (
259
291
expect . objectContaining ( {
260
292
metadata : { user : 'john' } ,
261
293
key : 'value' ,
@@ -267,13 +299,13 @@ describe('ContextLogger', () => {
267
299
268
300
it ( 'should not group context when contextKey provided but context object is empty' , ( ) => {
269
301
const logger = new ContextLogger ( MODULE_NAME ) ;
270
- ContextLogger . init ( mockLogger as any , { groupFields : { contextKey : 'metadata' } } ) ;
302
+ ContextLogger . init ( mockLogger , { groupFields : { contextKey : 'metadata' } } ) ;
271
303
const contextData = { } ;
272
304
273
305
jest . spyOn ( ContextStore , 'getContext' ) . mockReturnValue ( contextData ) ;
274
306
logger . info ( 'test message' , { key : 'value' } ) ;
275
307
276
- expect ( spyInfo ) . toHaveBeenCalledWith (
308
+ expect ( spyLog ) . toHaveBeenCalledWith (
277
309
{ key : 'value' } ,
278
310
'test message' ,
279
311
MODULE_NAME
@@ -282,9 +314,14 @@ describe('ContextLogger', () => {
282
314
} ) ;
283
315
284
316
describe ( 'with full grouping' , ( ) => {
317
+ beforeEach ( ( ) => {
318
+ // Reset the internal logger for each test to ensure clean state
319
+ ( ContextLogger as any ) . internalLogger = null ;
320
+ } ) ;
321
+
285
322
it ( 'should group both bindings and context under specified keys' , ( ) => {
286
323
const logger = new ContextLogger ( MODULE_NAME ) ;
287
- ContextLogger . init ( mockLogger as any , {
324
+ ContextLogger . init ( mockLogger , {
288
325
groupFields : {
289
326
bindingsKey : 'params' ,
290
327
contextKey : 'metadata'
@@ -295,7 +332,7 @@ describe('ContextLogger', () => {
295
332
jest . spyOn ( ContextStore , 'getContext' ) . mockReturnValue ( contextData ) ;
296
333
logger . info ( 'test message' , { key : 'value' } ) ;
297
334
298
- expect ( spyInfo ) . toHaveBeenCalledWith (
335
+ expect ( spyLog ) . toHaveBeenCalledWith (
299
336
expect . objectContaining ( {
300
337
params : { key : 'value' } ,
301
338
metadata : { user : 'john' } ,
@@ -307,7 +344,7 @@ describe('ContextLogger', () => {
307
344
308
345
it ( 'should not add specified keys when both bindings and context are empty' , ( ) => {
309
346
const logger = new ContextLogger ( MODULE_NAME ) ;
310
- ContextLogger . init ( mockLogger as any , {
347
+ ContextLogger . init ( mockLogger , {
311
348
groupFields : {
312
349
bindingsKey : 'params' ,
313
350
contextKey : 'metadata'
@@ -318,7 +355,7 @@ describe('ContextLogger', () => {
318
355
jest . spyOn ( ContextStore , 'getContext' ) . mockReturnValue ( contextData ) ;
319
356
logger . info ( 'test message' ) ;
320
357
321
- expect ( spyInfo ) . toHaveBeenCalledWith (
358
+ expect ( spyLog ) . toHaveBeenCalledWith (
322
359
{ } ,
323
360
'test message' ,
324
361
MODULE_NAME
@@ -328,9 +365,14 @@ describe('ContextLogger', () => {
328
365
} ) ;
329
366
330
367
describe ( 'context adaptation' , ( ) => {
368
+ beforeEach ( ( ) => {
369
+ // Reset the internal logger for each test to ensure clean state
370
+ ( ContextLogger as any ) . internalLogger = null ;
371
+ } ) ;
372
+
331
373
it ( 'should adapt context when adapter is provided' , ( ) => {
332
374
const logger = new ContextLogger ( MODULE_NAME ) ;
333
- ContextLogger . init ( mockLogger as any , {
375
+ ContextLogger . init ( mockLogger , {
334
376
contextAdapter : ( ctx ) => ( { user : ctx . user } )
335
377
} ) ;
336
378
const contextData = { user : 'john' , role : 'admin' } ;
@@ -339,7 +381,7 @@ describe('ContextLogger', () => {
339
381
340
382
logger . info ( 'test message' ) ;
341
383
342
- expect ( spyInfo ) . toHaveBeenCalledWith (
384
+ expect ( spyLog ) . toHaveBeenCalledWith (
343
385
expect . objectContaining ( {
344
386
user : 'john' , // Spread at root level since groupFields is disabled by default
345
387
} ) ,
@@ -355,7 +397,7 @@ describe('ContextLogger', () => {
355
397
jest . spyOn ( ContextStore , 'getContext' ) . mockReturnValue ( { } ) ;
356
398
logger . info ( 'test message' ) ;
357
399
358
- expect ( spyInfo ) . toHaveBeenCalledWith (
400
+ expect ( spyLog ) . toHaveBeenCalledWith (
359
401
expect . not . objectContaining ( {
360
402
bindings : undefined ,
361
403
context : undefined ,
@@ -367,9 +409,14 @@ describe('ContextLogger', () => {
367
409
} ) ;
368
410
369
411
describe ( 'bootstrap logs handling' , ( ) => {
412
+ beforeEach ( ( ) => {
413
+ // Reset the internal logger for each test to ensure clean state
414
+ ( ContextLogger as any ) . internalLogger = null ;
415
+ } ) ;
416
+
370
417
it ( 'should ignore bootstrap logs when ignoreBootstrapLogs is true' , ( ) => {
371
418
const logger = new ContextLogger ( MODULE_NAME ) ;
372
- ContextLogger . init ( mockLogger as any , { ignoreBootstrapLogs : true } ) ;
419
+ ContextLogger . init ( mockLogger , { ignoreBootstrapLogs : true } ) ;
373
420
374
421
// @ts -expect-error - Simulate bootstrap phase
375
422
logger . log ( 'Mapped {/api/users, GET} route' , 'RouterExplorer' ) ;
@@ -379,7 +426,7 @@ describe('ContextLogger', () => {
379
426
380
427
it ( 'should handle bootstrap logs when ignoreBootstrapLogs is false' , ( ) => {
381
428
const logger = new ContextLogger ( MODULE_NAME ) ;
382
- ContextLogger . init ( mockLogger as any , { ignoreBootstrapLogs : false } ) ;
429
+ ContextLogger . init ( mockLogger , { ignoreBootstrapLogs : false } ) ;
383
430
384
431
// @ts -expect-error - Simulate bootstrap phase
385
432
logger . log ( 'Mapped {/api/users, GET} route' , 'RouterExplorer' ) ;
@@ -393,7 +440,7 @@ describe('ContextLogger', () => {
393
440
394
441
it ( 'should handle bootstrap logs by default (ignoreBootstrapLogs not set)' , ( ) => {
395
442
const logger = new ContextLogger ( MODULE_NAME ) ;
396
- ContextLogger . init ( mockLogger as any , { } ) ;
443
+ ContextLogger . init ( mockLogger , { } ) ;
397
444
398
445
// @ts -expect-error - Simulate bootstrap phase
399
446
logger . log ( 'Mapped {/api/users, GET} route' , 'RouterExplorer' ) ;
0 commit comments