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

Commit 3117f7d

Browse files
author
Michal Vlasák
committed
Add serializer tests
1 parent 1ad504b commit 3117f7d

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

tests/serializers.test.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
const _ = require('lodash');
2+
const stream = require('stream');
3+
4+
let loggerFactory;
5+
6+
beforeEach(() => {
7+
jest.resetModules();
8+
loggerFactory = require('..'); // eslint-disable-line global-require
9+
});
10+
11+
test('Default serializers', () => {
12+
const error = {
13+
message: 'Bad error',
14+
code: 400,
15+
stack: 'Detailed stack',
16+
data: { user: 1 },
17+
devInfo: 'Lorem',
18+
userInfo: 'Ipsum',
19+
};
20+
const processEnv = {
21+
NODE_PATH: 'app:config',
22+
NODE_ENV: 'local',
23+
PATH: '.',
24+
USER: 'root',
25+
};
26+
const res = {
27+
out: 'Lorem ipsum',
28+
time: 14,
29+
noteToSelf: 'Send to user',
30+
};
31+
const req = {
32+
query: {
33+
password: '1234',
34+
passwordCheck: '1234',
35+
search: 'my cat',
36+
},
37+
body: {
38+
password: '1234',
39+
passwordCheck: '1234',
40+
catName: 'Cersei',
41+
},
42+
url: 'www.example.com',
43+
method: 'GET',
44+
extraData: 'Some server data',
45+
};
46+
47+
const logger = loggerFactory({
48+
streams: [
49+
{
50+
stream: new stream.Writable({
51+
write: (chunk, encoding, next) => {
52+
const json = JSON.parse(chunk);
53+
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+
});
58+
expect(json.req).toEqual(
59+
_.pick(
60+
_.omit(req, [
61+
'body.password',
62+
'body.passwordCheck',
63+
'query.password',
64+
'query.passwordCheck',
65+
]),
66+
['url', 'body', 'query', 'method']
67+
)
68+
);
69+
expect(json.res).toEqual(_.pick(res, ['out', 'time']));
70+
next();
71+
},
72+
}),
73+
},
74+
],
75+
});
76+
77+
logger.info({ error, processEnv, req, res });
78+
});
79+
80+
test('Disable custom path', () => {
81+
const req = {
82+
body: {},
83+
query: {},
84+
url: 'www.example.com',
85+
method: 'GET',
86+
extraData: 'Some server data',
87+
};
88+
89+
const logger = loggerFactory({
90+
disableFields: ['req.url'],
91+
streams: [
92+
{
93+
stream: new stream.Writable({
94+
write: (chunk, encoding, next) => {
95+
const json = JSON.parse(chunk);
96+
expect(json.req).toEqual(_.pick(req, ['body', 'query', 'method']));
97+
next();
98+
},
99+
}),
100+
},
101+
],
102+
});
103+
104+
logger.info({ req });
105+
});
106+
107+
test('Enable custom path', () => {
108+
const req = {
109+
body: {},
110+
query: {},
111+
url: 'www.example.com',
112+
method: 'GET',
113+
extraData: 'Some server data',
114+
};
115+
116+
const logger = loggerFactory({
117+
enableFields: ['req.extraData'],
118+
streams: [
119+
{
120+
stream: new stream.Writable({
121+
write: (chunk, encoding, next) => {
122+
const json = JSON.parse(chunk);
123+
expect(json.req).toEqual(_.pick(req, ['body', 'query', 'method', 'url', 'extraData']));
124+
next();
125+
},
126+
}),
127+
},
128+
],
129+
});
130+
131+
logger.info({ req });
132+
});

0 commit comments

Comments
 (0)