1
+ import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world' ;
1
2
import { Logger } from '../../src' ;
2
3
import { populateEnvironmentVariables } from '../helpers' ;
3
4
@@ -55,8 +56,13 @@ describe('Logger', () => {
55
56
56
57
logger . error ( 'foo' ) ;
57
58
logger . error ( 'foo' , { bar : 'baz' } ) ;
59
+ logger . error ( { bar : 'baz' , message : 'foo' } ) ;
58
60
59
- expect ( console . log ) . toBeCalledTimes ( 2 ) ;
61
+ const error = new Error ( 'Something happened!' ) ;
62
+ error . stack = 'A custom stack trace' ;
63
+ logger . error ( 'foo' , { bar : 'baz' } , error ) ;
64
+
65
+ expect ( console . log ) . toBeCalledTimes ( 4 ) ;
60
66
expect ( console . log ) . toHaveBeenNthCalledWith ( 1 , {
61
67
message : 'foo' ,
62
68
service : 'hello-world' ,
@@ -72,6 +78,27 @@ describe('Logger', () => {
72
78
timestamp : '2016-06-20T12:08:10.000Z' ,
73
79
xray_trace_id : 'abcdef123456abcdef123456abcdef123456'
74
80
} ) ;
81
+ expect ( console . log ) . toHaveBeenNthCalledWith ( 3 , {
82
+ bar : 'baz' ,
83
+ message : 'foo' ,
84
+ service : 'hello-world' ,
85
+ level : 'ERROR' ,
86
+ timestamp : '2016-06-20T12:08:10.000Z' ,
87
+ xray_trace_id : 'abcdef123456abcdef123456abcdef123456'
88
+ } ) ;
89
+ expect ( console . log ) . toHaveBeenNthCalledWith ( 4 , {
90
+ bar : 'baz' ,
91
+ error : {
92
+ message : 'Something happened!' ,
93
+ name : 'Error' ,
94
+ stack : 'A custom stack trace' ,
95
+ } ,
96
+ message : 'foo' ,
97
+ service : 'hello-world' ,
98
+ level : 'ERROR' ,
99
+ timestamp : '2016-06-20T12:08:10.000Z' ,
100
+ xray_trace_id : 'abcdef123456abcdef123456abcdef123456'
101
+ } ) ;
75
102
} ) ;
76
103
77
104
test ( 'should return a valid DEBUG log' , ( ) => {
@@ -125,5 +152,44 @@ describe('Logger', () => {
125
152
126
153
} ) ;
127
154
155
+ test ( 'should return a valid INFO log with context enabled' , ( ) => {
156
+
157
+ const logger = new Logger ( {
158
+ isContextEnabled : true
159
+ } ) ;
160
+ logger . addContext ( dummyContext ) ;
161
+
162
+ logger . info ( 'foo' ) ;
163
+ logger . info ( { message : 'foo' , bar : 'baz' } ) ;
164
+
165
+ expect ( console . log ) . toBeCalledTimes ( 2 ) ;
166
+ expect ( console . log ) . toHaveBeenNthCalledWith ( 1 , {
167
+ 'aws_request_id' : 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef' ,
168
+ 'cold_start' : true ,
169
+ 'lambda_function_arn' : 'arn:aws:lambda:eu-central-1:123456789012:function:Example' ,
170
+ 'lambda_function_memory_size' : 128 ,
171
+ 'lambda_function_name' : 'foo-bar-function' ,
172
+ 'level' : 'INFO' ,
173
+ 'message' : 'foo' ,
174
+ 'service' : 'hello-world' ,
175
+ 'timestamp' : '2016-06-20T12:08:10.000Z' ,
176
+ 'xray_trace_id' : 'abcdef123456abcdef123456abcdef123456'
177
+ } ) ;
178
+ expect ( console . log ) . toHaveBeenNthCalledWith ( 2 , {
179
+ 'aws_request_id' : 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef' ,
180
+ 'bar' : 'baz' ,
181
+ 'cold_start' : true ,
182
+ 'lambda_function_arn' : 'arn:aws:lambda:eu-central-1:123456789012:function:Example' ,
183
+ 'lambda_function_memory_size' : 128 ,
184
+ 'lambda_function_name' : 'foo-bar-function' ,
185
+ 'level' : 'INFO' ,
186
+ 'message' : 'foo' ,
187
+ 'service' : 'hello-world' ,
188
+ 'timestamp' : '2016-06-20T12:08:10.000Z' ,
189
+ 'xray_trace_id' : 'abcdef123456abcdef123456abcdef123456'
190
+ } ) ;
191
+
192
+ } ) ;
193
+
128
194
} ) ;
129
195
0 commit comments