1- import { logger } from '../src/index' ;
1+ import { Logger , logger } from '../src/index' ;
22
3- describe ( 'Logger' , ( ) => {
3+ describe ( 'Logger Configuration ' , ( ) => {
44 let consoleLogSpy : jest . SpyInstance ;
55 let consoleWarnSpy : jest . SpyInstance ;
66 let consoleErrorSpy : jest . SpyInstance ;
77 let consoleDebugSpy : jest . SpyInstance ;
8+ let customLogger : Logger ;
89
910 beforeEach ( ( ) => {
1011 consoleLogSpy = jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
@@ -14,84 +15,82 @@ describe('Logger', () => {
1415 } ) ;
1516
1617 afterEach ( ( ) => {
17- consoleLogSpy . mockRestore ( ) ;
18- consoleWarnSpy . mockRestore ( ) ;
19- consoleErrorSpy . mockRestore ( ) ;
20- consoleDebugSpy . mockRestore ( ) ;
18+ jest . restoreAllMocks ( ) ;
2119 } ) ;
2220
23- it ( "should log a message with an optional parameter when NODE_ENV is not 'production'" , ( ) => {
24- process . env . NODE_ENV = 'development' ;
25- const message = 'Test log message' ;
26- const additionalParam = { key : 'value' } ;
21+ it ( 'should use default log level "log" and log messages of that level or higher' , ( ) => {
22+ logger . debug ( 'This should not be logged' ) ;
23+ logger . log ( 'This should be logged' ) ;
24+ logger . warn ( 'This should be logged' ) ;
25+ logger . error ( 'This should be logged' ) ;
2726
28- logger . log ( message , additionalParam ) ;
27+ expect ( consoleDebugSpy ) . not . toHaveBeenCalled ( ) ;
28+ expect ( consoleLogSpy ) . toHaveBeenCalledTimes ( 1 ) ;
29+ expect ( consoleWarnSpy ) . toHaveBeenCalledTimes ( 1 ) ;
30+ expect ( consoleErrorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
31+ } ) ;
2932
30- expect ( consoleLogSpy ) . toHaveBeenCalledWith (
31- expect . stringMatching ( / \[ L O G \] .* : T e s t l o g m e s s a g e \{ " k e y " : " v a l u e " \} / )
32- ) ;
33+ it ( 'should allow instantiating a custom logger with log level "debug"' , ( ) => {
34+ customLogger = new Logger ( { logLevel : 'debug' } ) ;
35+
36+ customLogger . debug ( 'Debug message' ) ;
37+ customLogger . log ( 'Log message' ) ;
38+ customLogger . warn ( 'Warn message' ) ;
39+ customLogger . error ( 'Error message' ) ;
40+
41+ expect ( consoleDebugSpy ) . toHaveBeenCalledTimes ( 1 ) ;
42+ expect ( consoleLogSpy ) . toHaveBeenCalledTimes ( 1 ) ;
43+ expect ( consoleWarnSpy ) . toHaveBeenCalledTimes ( 1 ) ;
44+ expect ( consoleErrorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
3345 } ) ;
3446
35- it ( "should log a message without an optional parameter when NODE_ENV is not 'production'" , ( ) => {
36- process . env . NODE_ENV = 'development' ;
37- const message = 'Test log message' ;
47+ it ( 'should not log anything when isProduction is true' , ( ) => {
48+ customLogger = new Logger ( { logLevel : 'debug' , isProduction : true } ) ;
3849
39- logger . log ( message ) ;
50+ customLogger . debug ( 'Debug message' ) ;
51+ customLogger . log ( 'Log message' ) ;
52+ customLogger . warn ( 'Warn message' ) ;
53+ customLogger . error ( 'Error message' ) ;
4054
55+ expect ( consoleDebugSpy ) . not . toHaveBeenCalled ( ) ;
56+ expect ( consoleLogSpy ) . not . toHaveBeenCalled ( ) ;
57+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( ) ;
58+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( ) ;
59+ } ) ;
60+
61+ it ( 'should format log messages correctly' , ( ) => {
62+ logger . log ( 'Test log message' ) ;
4163 expect ( consoleLogSpy ) . toHaveBeenCalledWith (
4264 expect . stringMatching ( / \[ L O G \] .* : T e s t l o g m e s s a g e / )
4365 ) ;
4466 } ) ;
4567
46- it ( "should warn a message with an optional parameter when NODE_ENV is not 'production'" , ( ) => {
47- process . env . NODE_ENV = 'development' ;
48- const message = 'Test warn message' ;
49- const additionalParam = [ 1 , 2 , 3 ] ;
50-
51- logger . warn ( message , additionalParam ) ;
52-
68+ it ( 'should format warning messages correctly' , ( ) => {
69+ logger . warn ( 'Test warn message' ) ;
5370 expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
54- expect . stringMatching ( / \[ W A R N \] .* : T e s t w a r n m e s s a g e \[ 1 , 2 , 3 \] / )
71+ expect . stringMatching ( / \[ W A R N \] .* : T e s t w a r n m e s s a g e / )
5572 ) ;
5673 } ) ;
5774
58- it ( "should error a message with an optional parameter when NODE_ENV is not 'production'" , ( ) => {
59- process . env . NODE_ENV = 'development' ;
60- const message = 'Test error message' ;
61- const additionalParam = 'extra details' ;
62-
63- logger . error ( message , additionalParam ) ;
64-
75+ it ( 'should format error messages correctly' , ( ) => {
76+ logger . error ( 'Test error message' ) ;
6577 expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
66- expect . stringMatching ( / \[ E R R O R \] .* : T e s t e r r o r m e s s a g e e x t r a d e t a i l s / )
78+ expect . stringMatching ( / \[ E R R O R \] .* : T e s t e r r o r m e s s a g e / )
6779 ) ;
6880 } ) ;
6981
70- it ( "should debug a message with an optional parameter when NODE_ENV is not 'production'" , ( ) => {
71- process . env . NODE_ENV = 'development' ;
72- const message = 'Test debug message' ;
73- const additionalParam = { debug : true } ;
74-
75- logger . debug ( message , additionalParam ) ;
76-
82+ it ( 'should format debug messages correctly' , ( ) => {
83+ customLogger = new Logger ( { logLevel : 'debug' } ) ;
84+ customLogger . debug ( 'Test debug message' ) ;
7785 expect ( consoleDebugSpy ) . toHaveBeenCalledWith (
78- expect . stringMatching ( / \[ D E B U G \] .* : T e s t d e b u g m e s s a g e \{ " d e b u g " : t r u e \} / )
86+ expect . stringMatching ( / \[ D E B U G \] .* : T e s t d e b u g m e s s a g e / )
7987 ) ;
8088 } ) ;
8189
82- it ( "should not log, warn, error, or debug a message when NODE_ENV is 'production'" , ( ) => {
83- process . env . NODE_ENV = 'production' ;
84- const message = 'Test message' ;
85- const additionalParam = { ignored : true } ;
86-
87- logger . log ( message , additionalParam ) ;
88- logger . warn ( message , additionalParam ) ;
89- logger . error ( message , additionalParam ) ;
90- logger . debug ( message , additionalParam ) ;
91-
92- expect ( consoleLogSpy ) . not . toHaveBeenCalled ( ) ;
93- expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( ) ;
94- expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( ) ;
95- expect ( consoleDebugSpy ) . not . toHaveBeenCalled ( ) ;
90+ it ( 'should include additional parameters in log messages' , ( ) => {
91+ logger . log ( 'Log with params' , { key : 'value' } ) ;
92+ expect ( consoleLogSpy ) . toHaveBeenCalledWith (
93+ expect . stringMatching ( / \[ L O G \] .* : L o g w i t h p a r a m s \{ " k e y " : " v a l u e " \} / )
94+ ) ;
9695 } ) ;
9796} ) ;
0 commit comments