1
1
// @ts -check
2
- const logger = require ( '../../src/helpers/logger' )
2
+ const { error , info , verbose , log } = require ( '../../src/helpers/logger' )
3
3
const { expect, it } = require ( '@jest/globals' )
4
4
5
- describe ( 'Logger Helper' , ( ) => {
5
+ describe ( 'Logger Helper - Legacy log() tests ' , ( ) => {
6
6
afterEach ( ( ) => {
7
- jest . restoreAllMocks ( )
7
+ jest . clearAllMocks ( )
8
8
} )
9
9
10
10
it ( 'Should call logger with default options' , ( ) => {
11
11
// eslint-disable-next-line
12
12
jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } )
13
- logger . log ( 'message with no options' )
13
+ log ( 'message with no options' )
14
14
expect ( console . log ) . toHaveBeenCalledWith (
15
15
expect . stringMatching ( / n o o p t i o n s / ) ,
16
16
)
@@ -19,15 +19,15 @@ describe('Logger Helper', () => {
19
19
it ( 'Should not call logger with default options.level = debug and verbose not set' , ( ) => {
20
20
// eslint-disable-next-line
21
21
jest . spyOn ( console , 'debug' ) . mockImplementation ( ( ) => { } )
22
- logger . log ( 'message with debug level' , { level : 'debug' } )
22
+ log ( 'message with debug level' , { level : 'debug' } )
23
23
expect ( console . debug ) . not . toHaveBeenCalled ( )
24
24
} )
25
25
26
26
it ( 'Should call logger with options.level = debug and verbose set' , ( ) => {
27
27
jest . spyOn ( console , 'debug' ) . mockImplementation ( ( ) => {
28
28
// Intentionally empty
29
29
} )
30
- logger . log ( 'message with debug level and verbose' , {
30
+ log ( 'message with debug level and verbose' , {
31
31
level : 'debug' ,
32
32
args : { verbose : true } ,
33
33
} )
@@ -40,7 +40,7 @@ describe('Logger Helper', () => {
40
40
jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => {
41
41
// Intentionally empty
42
42
} )
43
- logger . log ( 'message with error level' , { level : 'error' } )
43
+ log ( 'message with error level' , { level : 'error' } )
44
44
expect ( console . error ) . toHaveBeenCalledWith (
45
45
expect . stringMatching ( / e r r o r l e v e l / ) ,
46
46
)
@@ -50,7 +50,63 @@ describe('Logger Helper', () => {
50
50
jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => {
51
51
// Intentionally empty
52
52
} )
53
- logger . log ( 'message with error level of foobar' , { level : 'foobar' } )
53
+ log ( 'message with error level of foobar' , { level : 'foobar' } )
54
54
expect ( console . log ) . toHaveBeenCalledWith ( expect . stringMatching ( / o f f o o b a r / ) )
55
55
} )
56
56
} )
57
+
58
+ describe ( 'Logging methods' , ( ) => {
59
+ afterEach ( ( ) => {
60
+ jest . clearAllMocks ( )
61
+ } )
62
+
63
+ it ( 'should call console.error() when error() is called' , ( ) => {
64
+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => {
65
+ // Intentionally empty
66
+ } )
67
+ error ( 'this is a test error' )
68
+ expect ( console . error ) . toHaveBeenCalledWith (
69
+ expect . stringMatching ( / \[ ' e r r o r ' \] / ) ,
70
+ )
71
+ expect ( console . error ) . toHaveBeenCalledWith (
72
+ expect . stringMatching ( / t h i s i s a t e s t e r r o r / ) ,
73
+ )
74
+ expect ( console . error ) . toHaveBeenCalledWith ( expect . stringMatching ( / Z \] / ) )
75
+ } )
76
+
77
+ it ( 'should call console.debug() when verbose() is called with shouldVerbose === true' , ( ) => {
78
+ jest . spyOn ( console , 'debug' ) . mockImplementation ( ( ) => {
79
+ // Intentionally empty
80
+ } )
81
+ verbose ( 'this is a test verbose' , true )
82
+ expect ( console . debug ) . toHaveBeenCalledWith (
83
+ expect . stringMatching ( / \[ ' v e r b o s e ' \] / ) ,
84
+ )
85
+ expect ( console . debug ) . toHaveBeenCalledWith (
86
+ expect . stringMatching ( / t h i s i s a t e s t v e r b o s e / ) ,
87
+ )
88
+ expect ( console . debug ) . toHaveBeenCalledWith ( expect . stringMatching ( / Z \] / ) )
89
+ } )
90
+
91
+ it ( 'should not call console.debug() when verbose() is called without shouldVerbose' , ( ) => {
92
+ jest . spyOn ( console , 'debug' ) . mockImplementation ( ( ) => {
93
+ // Intentionally empty
94
+ } )
95
+ verbose ( 'this is a test verbose' )
96
+ expect ( console . debug ) . not . toBeCalled ( )
97
+ } )
98
+
99
+ it ( 'should call console.log() when info() is called ' , ( ) => {
100
+ jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => {
101
+ // Intentionally empty
102
+ } )
103
+ info ( 'this is a test info' )
104
+ expect ( console . log ) . toHaveBeenCalledWith (
105
+ expect . stringMatching ( / \[ ' i n f o ' \] / ) ,
106
+ )
107
+ expect ( console . log ) . toHaveBeenCalledWith (
108
+ expect . stringMatching ( / t h i s i s a t e s t i n f o / ) ,
109
+ )
110
+ expect ( console . log ) . toHaveBeenCalledWith ( expect . stringMatching ( / Z \] / ) )
111
+ } )
112
+ } )
0 commit comments