Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 2d36a5f

Browse files
author
Michal Vlasák
committed
Do not flatten process variable, fix serializer tests, better default serializers
1 parent 337cabb commit 2d36a5f

2 files changed

Lines changed: 79 additions & 24 deletions

File tree

serializers.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@ const serializers = {
99
data: _.get(obj, 'data'),
1010
};
1111
},
12-
processEnv(obj) {
13-
return {
14-
nodePath: _.get(obj, 'NODE_PATH'),
15-
nodeEnv: _.get(obj, 'NODE_ENV'),
16-
};
12+
process(obj) {
13+
const nodePath = _.get(obj.env, 'NODE_PATH');
14+
const nodeEnv = _.get(obj.env, 'NODE_ENV');
15+
return _.omitBy(
16+
_.defaultsDeep({ env: _.omitBy({ nodePath, nodeEnv }, _.isUndefined) }, _.omit(obj, 'env')),
17+
val => _.isUndefined(val) || _.isEmpty(val)
18+
);
1719
},
1820
req(obj) {
1921
const omitFields = ['password', 'passwordCheck'];
2022
const [body, query] = ['body', 'query'].map(name => _.omit(_.get(obj, name), omitFields));
2123

22-
return {
23-
body,
24-
query,
25-
url: obj.originalUrl || obj.url,
26-
method: _.get(obj, 'method'),
27-
};
24+
return _.omitBy(
25+
{
26+
body,
27+
query,
28+
url: obj.originalUrl || obj.url,
29+
method: _.get(obj, 'method'),
30+
},
31+
val => _.isUndefined(val) || _.isEmpty(val)
32+
);
2833
},
2934
res(obj) {
3035
return {

tests/serializers.test.js

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ beforeEach(() => {
99
});
1010

1111
test('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

80128
test('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

107156
test('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

Comments
 (0)