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

Commit 4c286f0

Browse files
author
Michal Vlasák
committed
Fix current tests and add http ignore tests
1 parent aac6bf9 commit 4c286f0

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

tests/index.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ test('can create named logger', () => {
2121
});
2222

2323
test('can use custom stream', () => {
24+
const loggerWrites = jest.fn();
2425
const logger = loggerFactory({
2526
streams: [
2627
{
2728
stream: new stream.Writable({
2829
write: (chunk, encoding, next) => {
2930
const json = JSON.parse(chunk);
3031
expect(json.message).toBe('Hello');
32+
loggerWrites();
3133
next();
3234
},
3335
}),
@@ -36,9 +38,11 @@ test('can use custom stream', () => {
3638
});
3739

3840
logger.info('Hello');
41+
expect(loggerWrites).toBeCalled();
3942
});
4043

4144
test('can use warning level', () => {
45+
const loggerWrites = jest.fn();
4246
const logger = loggerFactory({
4347
streams: [
4448
{
@@ -47,6 +51,7 @@ test('can use warning level', () => {
4751
const json = JSON.parse(chunk);
4852
expect(json.message).toBe('Hello');
4953
expect(json.level).toBe(levels.warn);
54+
loggerWrites();
5055
next();
5156
},
5257
}),
@@ -55,6 +60,7 @@ test('can use warning level', () => {
5560
});
5661

5762
logger.warning('Hello');
63+
expect(loggerWrites).toBeCalled();
5864
});
5965

6066
test('express binds', () => {
@@ -64,3 +70,74 @@ test('express binds', () => {
6470
app.use(logger.express);
6571
return request.get('/');
6672
});
73+
74+
test('GET requests are logged by default', () => {
75+
const loggerWrites = jest.fn();
76+
const logger = loggerFactory({
77+
streams: [
78+
{
79+
stream: new stream.Writable({
80+
write: (chunk, encoding, next) => {
81+
const json = JSON.parse(chunk);
82+
expect(json.req.method).toBe('GET');
83+
loggerWrites();
84+
next();
85+
},
86+
}),
87+
},
88+
],
89+
});
90+
const app = express();
91+
const request = supertest(app);
92+
app.use(logger.express);
93+
return request.get('/').then(() => {
94+
expect(loggerWrites).toBeCalled();
95+
});
96+
});
97+
98+
test('OPTIONS requests are ignored by default', () => {
99+
const loggerWrites = jest.fn();
100+
const logger = loggerFactory({
101+
streams: [
102+
{
103+
stream: new stream.Writable({
104+
write: (chunk, encoding, next) => {
105+
loggerWrites();
106+
next();
107+
},
108+
}),
109+
},
110+
],
111+
});
112+
const app = express();
113+
const request = supertest(app);
114+
app.use(logger.express);
115+
return request.options('/').then(() => {
116+
expect(loggerWrites).not.toBeCalled();
117+
});
118+
});
119+
120+
['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'].forEach(method => {
121+
test(`${method} HTTP method can be ignored by options`, () => {
122+
const loggerWrites = jest.fn();
123+
const logger = loggerFactory({
124+
streams: [
125+
{
126+
stream: new stream.Writable({
127+
write: (chunk, encoding, next) => {
128+
loggerWrites();
129+
next();
130+
},
131+
}),
132+
},
133+
],
134+
ignoredHttpMethods: [method],
135+
});
136+
const app = express();
137+
const request = supertest(app);
138+
app.use(logger.express);
139+
return request[method.toLowerCase()]('/').then(() => {
140+
expect(loggerWrites).not.toBeCalled();
141+
});
142+
});
143+
});

0 commit comments

Comments
 (0)