forked from schibsted/middy-error-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
93 lines (72 loc) · 2.91 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const middy = require('@middy/core');
const createError = require('http-errors');
const middleware = require('./index');
const error = new createError.NotFound('File not found');
test('Middleware works with logging disabled', async () => {
const handler = middy(() => {
throw error;
});
handler.use(middleware({ logger: false }));
await expect(handler({}, {})).resolves.toMatchObject({
body: JSON.stringify({ statusCode: 404, message: 'File not found' }),
statusCode: 404,
});
});
test("Middleware doesn't log 404 errors - they happen a lot", async () => {
const mockLogger = {
error: jest.fn(() => {}),
};
const handler = middy(() => {
throw new createError.NotFound('File not found');
});
handler.use(middleware({ logger: mockLogger, filter: (err) => err.statusCode >= 500 }));
const response = await handler({}, {});
const { statusCode, message, stack } = JSON.parse(response.body);
expect(statusCode).toBe(404);
expect(message).toBe('File not found');
expect(stack).toBeUndefined();
expect(mockLogger.error).toHaveBeenCalledTimes(0);
});
test('Middleware logs and returns all error details', async () => {
const mockLogger = {
error: jest.fn(() => {}),
};
const handler = middy(() => {
throw new createError.ServiceUnavailable('Service not available');
});
handler.use(middleware({ logger: mockLogger, exposeStackTrace: true }));
const response = await handler({}, {});
const { statusCode, message, stack } = JSON.parse(response.body);
expect(statusCode).toBe(503);
expect(message).toBe('Service not available');
expect(stack).toBeDefined();
expect(mockLogger.error).toHaveBeenCalledTimes(1);
const [errorObject, errorMessage] = mockLogger.error.mock.calls[mockLogger.error.mock.calls.length - 1];
expect(errorMessage).toBe('ServiceUnavailableError: Service not available');
expect(errorObject.error.name).toBe('ServiceUnavailableError');
expect(errorObject.error.message).toBe('Service not available');
expect(errorObject.error.status).toBe(503);
expect(errorObject.error.statusCode).toBe(503);
expect(errorObject.error.expose).toBe(false);
expect(errorObject.error.stack).not.toBeNull();
});
test('Keep data already present in response', async () => {
const handler = middy(async () => {
throw error;
});
handler.use(middleware({ logger: false }));
// eslint-disable-next-line no-shadow
handler.onError(async (handler) => {
// eslint-disable-next-line no-param-reassign
handler.response = {
headers: {
someHeader: 'someValue',
},
};
});
await expect(handler({}, {})).resolves.toMatchObject({
body: JSON.stringify({ statusCode: 404, message: 'File not found' }),
statusCode: 404,
headers: { someHeader: 'someValue' },
});
});