1
|
|
|
import { flushPromises } from '@test/utils/testUtils'; |
2
|
|
|
import * as http from 'http'; |
3
|
|
|
import { Request, Response } from 'express'; |
4
|
|
|
import request from 'supertest'; |
5
|
|
|
import ExpressBeans from '@/ExpressBeans'; |
6
|
|
|
import { Route, RouterBean } from '@/main'; |
7
|
|
|
|
8
|
|
|
describe('Error Handling integration tests', () => { |
9
|
|
|
let server: http.Server; |
10
|
|
|
let application: ExpressBeans; |
11
|
|
|
beforeEach(() => { |
12
|
|
|
jest.resetModules(); |
13
|
|
|
}); |
14
|
|
|
|
15
|
|
|
afterEach(() => { |
16
|
|
|
server.close(); |
17
|
|
|
}); |
18
|
|
|
|
19
|
|
|
test('error handling on synchronous routes', async () => { |
20
|
|
|
// GIVEN |
21
|
|
|
@RouterBean('/test') |
22
|
|
|
class TestRouter { |
23
|
|
|
@Route('GET', '/42') |
24
|
|
|
test(_req: Request, res: Response) { |
25
|
|
|
res.send('42 is the answer'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
@Route('GET', '/error') |
29
|
|
|
error(_req: Request, _res: Response) { |
30
|
|
|
throw new Error('ops!'); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
application = new ExpressBeans({ listen: false, routerBeans: [TestRouter] }); |
34
|
|
|
await flushPromises(); |
35
|
|
|
server = application.listen(3001); |
36
|
|
|
await flushPromises(); |
37
|
|
|
|
38
|
|
|
// WHEN |
39
|
|
|
await request(server).get('/test/error').expect(500); |
40
|
|
|
|
41
|
|
|
// THEN |
42
|
|
|
const { text } = await request(server).get('/test/42').expect(200); |
43
|
|
|
expect(text).toBe('42 is the answer'); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
test('error handling on asynchronous routes', async () => { |
47
|
|
|
// GIVEN |
48
|
|
|
@RouterBean('/test') |
49
|
|
|
class TestRouter { |
50
|
|
|
@Route('GET', '/42') |
51
|
|
|
test(_req: Request, res: Response) { |
52
|
|
|
res.send('42 is the answer'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
@Route('GET', '/error') |
56
|
|
|
async error(_req: Request, _res: Response) { |
57
|
|
|
throw new Error('ops!'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
application = new ExpressBeans({ listen: false, routerBeans: [TestRouter] }); |
61
|
|
|
await flushPromises(); |
62
|
|
|
server = application.listen(3001); |
63
|
|
|
await flushPromises(); |
64
|
|
|
|
65
|
|
|
// WHEN |
66
|
|
|
await request(server).get('/test/error').expect(500); |
67
|
|
|
|
68
|
|
|
// THEN |
69
|
|
|
const { text } = await request(server).get('/test/42').expect(200); |
70
|
|
|
expect(text).toBe('42 is the answer'); |
71
|
|
|
}); |
72
|
|
|
}); |
73
|
|
|
|