1
- import { LogLevel , log , setLogLevel , setCI , data , print , error , warning , success , debug , trace , prefix , withCorkedLogging } from '../lib/logging' ;
1
+ import { LogLevel , log , setLogLevel , setCI , data , print , error , warning , success , debug , trace , prefix , withCorkedLogging } from '../../../ lib/logging' ;
2
2
3
3
describe ( 'logging' , ( ) => {
4
4
// Mock streams to capture output
5
5
let mockStdout : jest . Mock ;
6
6
let mockStderr : jest . Mock ;
7
7
8
+ // Helper function to strip ANSI codes
9
+ const stripAnsi = ( str : string ) : string => {
10
+ const ansiRegex = / \u001b \[ [ 0 - 9 ; ] * [ a - z A - Z ] / g;
11
+ return str . replace ( ansiRegex , '' ) ;
12
+ } ;
13
+
8
14
beforeEach ( ( ) => {
9
15
// Reset log level before each test
10
16
setLogLevel ( LogLevel . INFO ) ;
@@ -14,44 +20,45 @@ describe('logging', () => {
14
20
mockStdout = jest . fn ( ) ;
15
21
mockStderr = jest . fn ( ) ;
16
22
17
- // Mock the write methods directly
23
+ // Mock the write methods directly and strip ANSI codes
18
24
jest . spyOn ( process . stdout , 'write' ) . mockImplementation ( ( chunk : any ) => {
19
- mockStdout ( chunk . toString ( ) ) ;
25
+ mockStdout ( stripAnsi ( chunk . toString ( ) ) ) ;
20
26
return true ;
21
27
} ) ;
22
28
23
29
jest . spyOn ( process . stderr , 'write' ) . mockImplementation ( ( chunk : any ) => {
24
- mockStderr ( chunk . toString ( ) ) ;
30
+ mockStderr ( stripAnsi ( chunk . toString ( ) ) ) ;
25
31
return true ;
26
32
} ) ;
27
33
} ) ;
28
34
29
35
afterEach ( ( ) => {
30
36
jest . restoreAllMocks ( ) ;
31
37
} ) ;
38
+
32
39
describe ( 'stream selection' , ( ) => {
33
40
test ( 'data() always writes to stdout' , ( ) => {
34
41
data ( 'test message' ) ;
35
- expect ( mockStdout ) . toHaveBeenCalledWith ( expect . stringContaining ( 'test message\n' ) ) ;
42
+ expect ( mockStdout ) . toHaveBeenCalledWith ( 'test message\n' ) ;
36
43
expect ( mockStderr ) . not . toHaveBeenCalled ( ) ;
37
44
} ) ;
38
45
39
46
test ( 'error() always writes to stderr' , ( ) => {
40
47
error ( 'test error' ) ;
41
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'test error\n' ) ) ;
48
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'test error\n' ) ;
42
49
expect ( mockStdout ) . not . toHaveBeenCalled ( ) ;
43
50
} ) ;
44
51
45
52
test ( 'print() writes to stderr by default' , ( ) => {
46
53
print ( 'test print' ) ;
47
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'test print\n' ) ) ;
54
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'test print\n' ) ;
48
55
expect ( mockStdout ) . not . toHaveBeenCalled ( ) ;
49
56
} ) ;
50
57
51
58
test ( 'print() writes to stdout in CI mode' , ( ) => {
52
59
setCI ( true ) ;
53
60
print ( 'test print' ) ;
54
- expect ( mockStdout ) . toHaveBeenCalledWith ( expect . stringContaining ( 'test print\n' ) ) ;
61
+ expect ( mockStdout ) . toHaveBeenCalledWith ( 'test print\n' ) ;
55
62
expect ( mockStderr ) . not . toHaveBeenCalled ( ) ;
56
63
} ) ;
57
64
} ) ;
@@ -62,9 +69,9 @@ describe('logging', () => {
62
69
error ( 'error message' ) ;
63
70
warning ( 'warning message' ) ;
64
71
print ( 'print message' ) ;
65
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'error message\n' ) ) ;
66
- expect ( mockStderr ) . not . toHaveBeenCalledWith ( expect . stringContaining ( 'warning message\n' ) ) ;
67
- expect ( mockStderr ) . not . toHaveBeenCalledWith ( expect . stringContaining ( 'print message\n' ) ) ;
72
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'error message\n' ) ;
73
+ expect ( mockStderr ) . not . toHaveBeenCalledWith ( 'warning message\n' ) ;
74
+ expect ( mockStderr ) . not . toHaveBeenCalledWith ( 'print message\n' ) ;
68
75
} ) ;
69
76
70
77
test ( 'debug messages only show at debug level' , ( ) => {
@@ -74,7 +81,7 @@ describe('logging', () => {
74
81
75
82
setLogLevel ( LogLevel . DEBUG ) ;
76
83
debug ( 'debug message' ) ;
77
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'debug message\n' ) ) ;
84
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'debug message\n' ) ;
78
85
} ) ;
79
86
80
87
test ( 'trace messages only show at trace level' , ( ) => {
@@ -84,26 +91,25 @@ describe('logging', () => {
84
91
85
92
setLogLevel ( LogLevel . TRACE ) ;
86
93
trace ( 'trace message' ) ;
87
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'trace message\n' ) ) ;
94
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'trace message\n' ) ;
88
95
} ) ;
89
96
} ) ;
90
97
91
98
describe ( 'message formatting' , ( ) => {
92
99
test ( 'formats messages with multiple arguments' , ( ) => {
93
100
print ( 'Value: %d, String: %s' , 42 , 'test' ) ;
94
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Value: 42, String: test\n' ) ) ;
101
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'Value: 42, String: test\n' ) ;
95
102
} ) ;
96
103
97
104
test ( 'handles prefix correctly' , ( ) => {
98
105
const prefixedLog = prefix ( 'PREFIX' ) ;
99
106
prefixedLog ( 'test message' ) ;
100
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'PREFIX test message\n' ) ) ;
107
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'PREFIX test message\n' ) ;
101
108
} ) ;
102
109
103
110
test ( 'handles custom styles' , ( ) => {
104
111
success ( 'success message' ) ;
105
- // Note: actual styling will depend on chalk, but we can verify the message is there
106
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'success message\n' ) ) ;
112
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'success message\n' ) ;
107
113
} ) ;
108
114
} ) ;
109
115
@@ -115,8 +121,8 @@ describe('logging', () => {
115
121
expect ( mockStderr ) . not . toHaveBeenCalled ( ) ;
116
122
} ) ;
117
123
118
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'message 1\n' ) ) ;
119
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'message 2\n' ) ) ;
124
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'message 1\n' ) ;
125
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'message 2\n' ) ;
120
126
} ) ;
121
127
122
128
test ( 'handles nested corking correctly' , async ( ) => {
@@ -130,9 +136,9 @@ describe('logging', () => {
130
136
} ) ;
131
137
132
138
expect ( mockStderr ) . toHaveBeenCalledTimes ( 3 ) ;
133
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'outer 1\n' ) ) ;
134
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'inner\n' ) ) ;
135
- expect ( mockStderr ) . toHaveBeenCalledWith ( expect . stringContaining ( 'outer 2\n' ) ) ;
139
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'outer 1\n' ) ;
140
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'inner\n' ) ;
141
+ expect ( mockStderr ) . toHaveBeenCalledWith ( 'outer 2\n' ) ;
136
142
} ) ;
137
143
} ) ;
138
144
@@ -145,7 +151,7 @@ describe('logging', () => {
145
151
prefix : 'PREFIX' ,
146
152
} ) ;
147
153
expect ( mockStderr ) . toHaveBeenCalledWith (
148
- expect . stringMatching ( / P R E F I X \[ \d { 2 } : \d { 2 } : \d { 2 } \] t e s t m e s s a g e \n / ) ,
154
+ expect . stringMatching ( / ^ P R E F I X \[ \d { 2 } : \d { 2 } : \d { 2 } \] t e s t m e s s a g e \n $ / ) ,
149
155
) ;
150
156
} ) ;
151
157
} ) ;
0 commit comments