@@ -22,7 +22,7 @@ import {
22
22
} from '.' ;
23
23
import { partial } from '~test/util' ;
24
24
25
- const initialContext = 'initial_context' ;
25
+ const logContext = 'initial_context' ;
26
26
27
27
vi . unmock ( '.' ) ;
28
28
vi . mock ( 'nanoid' , ( ) => ( {
@@ -39,10 +39,7 @@ describe('logger/index', () => {
39
39
it ( 'uses an auto-generated log context' , ( ) => {
40
40
logger . debug ( '' ) ;
41
41
42
- expect ( bunyanDebugSpy ) . toHaveBeenCalledWith (
43
- { logContext : initialContext } ,
44
- '' ,
45
- ) ;
42
+ expect ( bunyanDebugSpy ) . toHaveBeenCalledWith ( { logContext } , '' ) ;
46
43
} ) ;
47
44
48
45
it ( 'sets and gets context' , ( ) => {
@@ -70,95 +67,127 @@ describe('logger/index', () => {
70
67
71
68
describe ( 'meta functions' , ( ) => {
72
69
beforeEach ( ( ) => {
73
- setContext ( initialContext ) ;
70
+ setContext ( logContext ) ;
71
+ setMeta ( { } ) ;
74
72
} ) ;
75
73
76
74
it ( 'sets meta' , ( ) => {
77
- const logMeta = { foo : 'foo' } ;
78
- const meta = { bar : 'bar' } ;
79
- setMeta ( meta ) ;
75
+ setMeta ( { foo : 'foo' } ) ;
76
+ setMeta ( { bar : 'bar' } ) ;
80
77
81
- logger . debug ( logMeta , '' ) ;
78
+ logger . debug ( { baz : 'baz' } , 'message ' ) ;
82
79
83
80
expect ( bunyanDebugSpy ) . toHaveBeenCalledWith (
84
- { logContext : initialContext , ...meta , ...logMeta } ,
85
- '' ,
81
+ {
82
+ logContext,
83
+ // `foo` key was rewritten
84
+ bar : 'bar' ,
85
+ baz : 'baz' ,
86
+ } ,
87
+ 'message' ,
86
88
) ;
87
89
expect ( bunyanDebugSpy ) . toHaveBeenCalledTimes ( 1 ) ;
88
90
} ) ;
89
91
90
92
it ( 'adds meta' , ( ) => {
91
- const logMeta = { foo : 'foo' } ;
92
- const meta = { bar : 'bar' } ;
93
- addMeta ( meta ) ;
93
+ setMeta ( { foo : 'foo' } ) ;
94
+ addMeta ( { bar : 'bar' } ) ;
94
95
95
- logger . debug ( logMeta , '' ) ;
96
+ logger . debug ( { baz : 'baz' } , 'message ' ) ;
96
97
97
98
expect ( bunyanDebugSpy ) . toHaveBeenCalledWith (
98
- { logContext : initialContext , ...meta , ...logMeta } ,
99
- '' ,
99
+ {
100
+ logContext,
101
+ foo : 'foo' ,
102
+ bar : 'bar' ,
103
+ baz : 'baz' ,
104
+ } ,
105
+ 'message' ,
100
106
) ;
101
107
expect ( bunyanDebugSpy ) . toHaveBeenCalledTimes ( 1 ) ;
102
108
} ) ;
103
109
104
110
it ( 'removes meta' , ( ) => {
105
- const logMeta = { foo : 'foo' } ;
106
- const meta = { bar : 'bar' } ;
107
- setMeta ( meta ) ;
111
+ setMeta ( {
112
+ foo : 'foo' ,
113
+ bar : 'bar' ,
114
+ } ) ;
108
115
109
- logger . debug ( logMeta , '' ) ;
116
+ logger . debug ( { baz : 'baz' } , 'message ' ) ;
110
117
111
118
expect ( bunyanDebugSpy ) . toHaveBeenCalledWith (
112
- { logContext : initialContext , ...meta , ...logMeta } ,
113
- '' ,
119
+ {
120
+ logContext,
121
+ foo : 'foo' ,
122
+ bar : 'bar' ,
123
+ baz : 'baz' ,
124
+ } ,
125
+ 'message' ,
114
126
) ;
115
127
expect ( bunyanDebugSpy ) . toHaveBeenCalledTimes ( 1 ) ;
116
128
117
- removeMeta ( Object . keys ( meta ) ) ;
129
+ removeMeta ( [ 'bar' ] ) ;
118
130
119
- logger . debug ( logMeta , '' ) ;
131
+ logger . debug ( { baz : 'baz' } , 'message ' ) ;
120
132
121
133
expect ( bunyanDebugSpy ) . toHaveBeenCalledWith (
122
- { logContext : initialContext , ...logMeta } ,
123
- '' ,
134
+ {
135
+ logContext,
136
+ foo : 'foo' ,
137
+ baz : 'baz' ,
138
+ } ,
139
+ 'message' ,
124
140
) ;
125
141
expect ( bunyanDebugSpy ) . toHaveBeenCalledTimes ( 2 ) ;
126
142
} ) ;
127
143
128
144
it ( 'withMeta adds and removes metadata correctly' , ( ) => {
129
- const logMeta = { foo : 'foo' } ;
130
- const tempMeta = { bar : 'bar' } ;
145
+ setMeta ( { foo : 'foo' } ) ;
131
146
132
- withMeta ( tempMeta , ( ) => {
133
- logger . debug ( logMeta , '' ) ;
147
+ withMeta ( { bar : 'bar' } , ( ) => {
148
+ logger . debug ( { baz : 'baz' } , 'message ' ) ;
134
149
expect ( bunyanDebugSpy ) . toHaveBeenCalledWith (
135
- { logContext : initialContext , ...tempMeta , ...logMeta } ,
136
- '' ,
150
+ {
151
+ logContext,
152
+ foo : 'foo' ,
153
+ bar : 'bar' ,
154
+ baz : 'baz' ,
155
+ } ,
156
+ 'message' ,
137
157
) ;
138
158
} ) ;
139
159
140
- logger . debug ( logMeta , '' ) ;
160
+ logger . debug ( { baz : 'baz' } , 'message ' ) ;
141
161
expect ( bunyanDebugSpy ) . toHaveBeenCalledWith (
142
- { logContext : initialContext , ...logMeta } ,
143
- '' ,
162
+ {
163
+ logContext,
164
+ foo : 'foo' ,
165
+ bar : 'bar' ,
166
+ baz : 'baz' ,
167
+ } ,
168
+ 'message' ,
144
169
) ;
145
170
} ) ;
146
171
147
172
it ( 'withMeta handles cleanup when callback throws' , ( ) => {
148
- const logMeta = { foo : 'foo' } ;
149
- const tempMeta = { bar : 'bar' } ;
173
+ setMeta ( { foo : 'foo' } ) ;
150
174
151
175
expect ( ( ) =>
152
- withMeta ( tempMeta , ( ) => {
153
- logger . debug ( logMeta , '' ) ;
176
+ withMeta ( { bar : 'bar' } , ( ) => {
177
+ logger . debug ( { baz : 'baz' } , 'message ' ) ;
154
178
throw new Error ( 'test error' ) ;
155
179
} ) ,
156
180
) . toThrow ( 'test error' ) ;
157
181
158
- logger . debug ( logMeta , '' ) ;
182
+ logger . debug ( { baz : 'baz' } , 'message ' ) ;
159
183
expect ( bunyanDebugSpy ) . toHaveBeenCalledWith (
160
- { logContext : initialContext , ...logMeta } ,
161
- '' ,
184
+ {
185
+ logContext,
186
+ foo : 'foo' ,
187
+ bar : 'bar' ,
188
+ baz : 'baz' ,
189
+ } ,
190
+ 'message' ,
162
191
) ;
163
192
} ) ;
164
193
} ) ;
0 commit comments