@@ -9,6 +9,7 @@ beforeEach(() => {
99} ) ;
1010
1111test ( 'Default serializers' , ( ) => {
12+ const loggerWrites = jest . fn ( ) ;
1213 const error = {
1314 message : 'Bad error' ,
1415 code : 400 ,
@@ -17,11 +18,13 @@ test('Default serializers', () => {
1718 devInfo : 'Lorem' ,
1819 userInfo : 'Ipsum' ,
1920 } ;
20- const processEnv = {
21- NODE_PATH : 'app:config' ,
22- NODE_ENV : 'local' ,
23- PATH : '.' ,
24- USER : 'root' ,
21+ const process = {
22+ env : {
23+ NODE_PATH : 'app:config' ,
24+ NODE_ENV : 'local' ,
25+ PATH : '.' ,
26+ USER : 'root' ,
27+ } ,
2528 } ;
2629 const res = {
2730 out : 'Lorem ipsum' ,
@@ -51,9 +54,9 @@ test('Default serializers', () => {
5154 write : ( chunk , encoding , next ) => {
5255 const json = JSON . parse ( chunk ) ;
5356 expect ( json . error ) . toEqual ( _ . pick ( error , [ 'message' , 'code' , 'stack' , 'data' ] ) ) ;
54- expect ( json . processEnv ) . toEqual ( {
55- nodePath : processEnv . NODE_PATH ,
56- nodeEnv : processEnv . NODE_ENV ,
57+ expect ( json . process . env ) . toEqual ( {
58+ nodePath : process . env . NODE_PATH ,
59+ nodeEnv : process . env . NODE_ENV ,
5760 } ) ;
5861 expect ( json . req ) . toEqual (
5962 _ . pick (
@@ -67,20 +70,64 @@ test('Default serializers', () => {
6770 )
6871 ) ;
6972 expect ( json . res ) . toEqual ( _ . pick ( res , [ 'out' , 'time' ] ) ) ;
73+ loggerWrites ( ) ;
74+ next ( ) ;
75+ } ,
76+ } ) ,
77+ } ,
78+ ] ,
79+ } ) ;
80+
81+ logger . info ( { error, process, req, res } ) ;
82+ expect ( loggerWrites ) . toBeCalled ( ) ;
83+ } ) ;
84+
85+ test ( 'No extra fields are added in default serializers' , ( ) => {
86+ const loggerWrites = jest . fn ( ) ;
87+ const error = {
88+ devInfo : 'Lorem' ,
89+ userInfo : 'Ipsum' ,
90+ } ;
91+ const process = {
92+ stuff : {
93+ NODE_PATH : 'app:config' ,
94+ NODE_ENV : 'local' ,
95+ PATH : '.' ,
96+ USER : 'root' ,
97+ } ,
98+ } ;
99+ const res = {
100+ noteToSelf : 'Send to user' ,
101+ } ;
102+ const req = {
103+ extraData : 'Some server data' ,
104+ } ;
105+
106+ const logger = loggerFactory ( {
107+ streams : [
108+ {
109+ stream : new stream . Writable ( {
110+ write : ( chunk , encoding , next ) => {
111+ const json = JSON . parse ( chunk ) ;
112+ expect ( json . error ) . toEqual ( { } ) ;
113+ expect ( json . res ) . toEqual ( { } ) ;
114+ expect ( json . req ) . toEqual ( { } ) ;
115+ expect ( json . process ) . toEqual ( process ) ;
116+ loggerWrites ( ) ;
70117 next ( ) ;
71118 } ,
72119 } ) ,
73120 } ,
74121 ] ,
75122 } ) ;
76123
77- logger . info ( { error, processEnv, req, res } ) ;
124+ logger . info ( { error, process, req, res } ) ;
125+ expect ( loggerWrites ) . toBeCalled ( ) ;
78126} ) ;
79127
80128test ( 'Disable custom path' , ( ) => {
129+ const loggerWrites = jest . fn ( ) ;
81130 const req = {
82- body : { } ,
83- query : { } ,
84131 url : 'www.example.com' ,
85132 method : 'GET' ,
86133 extraData : 'Some server data' ,
@@ -94,6 +141,7 @@ test('Disable custom path', () => {
94141 write : ( chunk , encoding , next ) => {
95142 const json = JSON . parse ( chunk ) ;
96143 expect ( json . req ) . toEqual ( _ . pick ( req , [ 'body' , 'query' , 'method' ] ) ) ;
144+ loggerWrites ( ) ;
97145 next ( ) ;
98146 } ,
99147 } ) ,
@@ -102,12 +150,12 @@ test('Disable custom path', () => {
102150 } ) ;
103151
104152 logger . info ( { req } ) ;
153+ expect ( loggerWrites ) . toBeCalled ( ) ;
105154} ) ;
106155
107156test ( 'Enable custom path' , ( ) => {
157+ const loggerWrites = jest . fn ( ) ;
108158 const req = {
109- body : { } ,
110- query : { } ,
111159 url : 'www.example.com' ,
112160 method : 'GET' ,
113161 extraData : 'Some server data' ,
@@ -121,6 +169,7 @@ test('Enable custom path', () => {
121169 write : ( chunk , encoding , next ) => {
122170 const json = JSON . parse ( chunk ) ;
123171 expect ( json . req ) . toEqual ( _ . pick ( req , [ 'body' , 'query' , 'method' , 'url' , 'extraData' ] ) ) ;
172+ loggerWrites ( ) ;
124173 next ( ) ;
125174 } ,
126175 } ) ,
@@ -129,4 +178,5 @@ test('Enable custom path', () => {
129178 } ) ;
130179
131180 logger . info ( { req } ) ;
181+ expect ( loggerWrites ) . toBeCalled ( ) ;
132182} ) ;
0 commit comments