-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.tests.js
266 lines (209 loc) · 7.65 KB
/
index.tests.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const express3 = require('express3');
const express4 = require('express4');
const express5 = require('express5');
const expressListRoutes = require('./index');
function handler(req, res) {
res.send('handled');
}
describe('express 3', () => {
it('prints out all routes', () => {
const logger = jest.fn();
const app = express3();
app.get('/test', handler);
app.get('/user', handler);
app.post('/user', handler);
app.patch('/user', handler);
app.delete('/user', handler);
const paths = expressListRoutes(app, { logger });
expect(paths).toEqual([
{ method: 'GET', path: '/test' },
{ method: 'GET', path: '/user' },
{ method: 'POST', path: '/user' },
{ method: 'PATCH', path: '/user' },
{ method: 'DELETE', path: '/user' },
]);
expect(logger.mock.calls).toEqual([
['\u001b[32mGET\u001b[39m', ' ', '/test'],
['\u001b[32mGET\u001b[39m', ' ', '/user'],
['\u001b[33mPOST\u001b[39m', ' ', '/user'],
['\u001b[90mPATCH\u001b[39m', ' ', '/user'],
['\u001b[31mDELETE\u001b[39m', ' ', '/user'],
]);
});
it('works with prefix and dividers', () => {
const logger = jest.fn();
const app = express3();
app.get('/test', handler);
app.post('/user', handler);
app.get('/user', handler);
const paths = expressListRoutes(app, { logger, prefix: '/api/v1' });
expect(paths).toEqual([
{ method: 'GET', path: '/api/v1/test' },
{ method: 'GET', path: '/api/v1/user' },
{ method: 'POST', path: '/api/v1/user' },
]);
expect(logger.mock.calls).toEqual([
['\u001b[32mGET\u001b[39m', ' ', '/api/v1/test'],
['\u001b[32mGET\u001b[39m', ' ', '/api/v1/user'],
['\u001b[33mPOST\u001b[39m', ' ', '/api/v1/user'],
]);
});
});
describe('express 4', () => {
it('works with top level routes', () => {
const logger = jest.fn();
const app = express4();
app.get('/user', handler);
app.post('/user', handler);
const paths = expressListRoutes(app, { logger });
expect(paths).toEqual([
{ method: 'GET', path: '/user' },
{ method: 'POST', path: '/user' },
]);
expect(logger.mock.calls).toEqual([
['\u001b[32mGET\u001b[39m', ' ', '/user'],
['\u001b[33mPOST\u001b[39m', ' ', '/user'],
]);
});
it('works with express.Router', () => {
const logger = jest.fn();
const app = express4();
const router = express4.Router();
app.get('/test', handler);
router.route('/user').post(handler).get(handler).put(handler);
app.use(router);
const paths = expressListRoutes(router, { logger });
expect(paths).toEqual([
{ method: 'POST', path: '/user' },
{ method: 'GET', path: '/user' },
{ method: 'PUT', path: '/user' },
]);
expect(logger.mock.calls).toEqual([
['\u001b[33mPOST\u001b[39m', ' ', '/user'],
['\u001b[32mGET\u001b[39m', ' ', '/user'],
['\u001b[34mPUT\u001b[39m', ' ', '/user'],
]);
});
it('handles nested routers', () => {
const logger = jest.fn();
const app = express4();
const router = express4.Router();
app.get('/test', handler);
router.route('/user').post(handler).get(handler).put(handler);
app.use('/admin', router);
const paths = expressListRoutes(app, { logger, prefix: '/api/v1' });
expect(paths).toEqual([
{ method: 'GET', path: '/api/v1/test' },
{ method: 'POST', path: '/api/v1/admin/user' },
{ method: 'GET', path: '/api/v1/admin/user' },
{ method: 'PUT', path: '/api/v1/admin/user' },
]);
expect(logger.mock.calls).toEqual([
['\u001b[32mGET\u001b[39m', ' ', '/api/v1/test'],
['\u001b[33mPOST\u001b[39m', ' ', '/api/v1/admin/user'],
['\u001b[32mGET\u001b[39m', ' ', '/api/v1/admin/user'],
['\u001b[34mPUT\u001b[39m', ' ', '/api/v1/admin/user'],
]);
});
it('works with options', () => {
const logger = jest.fn();
const app = express4();
app.get('/user', handler);
app.post('/user', handler);
const paths = expressListRoutes(app, { logger, prefix: '/api/v1', spacer: 3 });
expect(paths).toEqual([
{ method: 'GET', path: '/api/v1/user' },
{ method: 'POST', path: '/api/v1/user' },
]);
expect(logger.mock.calls).toEqual([
['\u001b[32mGET\u001b[39m', '', '/api/v1/user'],
['\u001b[33mPOST\u001b[39m', '', '/api/v1/user'],
]);
});
});
describe('express 5', () => {
it('works with top level routes', () => {
const logger = jest.fn();
const app = express5();
app.get('/user', handler);
app.post('/user', handler);
const paths = expressListRoutes(app, { logger });
expect(paths).toEqual([
{ method: 'GET', path: '/user' },
{ method: 'POST', path: '/user' },
]);
expect(logger.mock.calls).toEqual([
['\u001b[32mGET\u001b[39m', ' ', '/user'],
['\u001b[33mPOST\u001b[39m', ' ', '/user'],
]);
});
it('works with express.Router', () => {
const logger = jest.fn();
const app = express5();
const router = express5.Router();
app.get('/test', handler);
router.route('/user').post(handler).get(handler).put(handler);
app.use(router);
const paths = expressListRoutes(router, { logger });
expect(paths).toEqual([
{ method: 'POST', path: '/user' },
{ method: 'GET', path: '/user' },
{ method: 'PUT', path: '/user' },
]);
expect(logger.mock.calls).toEqual([
['\u001b[33mPOST\u001b[39m', ' ', '/user'],
['\u001b[32mGET\u001b[39m', ' ', '/user'],
['\u001b[34mPUT\u001b[39m', ' ', '/user'],
]);
});
it('handles nested routers', () => {
const logger = jest.fn();
const app = express5();
const router = express5.Router();
app.get('/test', handler);
router.route('/user').post(handler).get(handler).put(handler);
app.use('/admin', router);
expressListRoutes(app, { logger, prefix: '/api/v1' });
expect(logger.mock.calls).toEqual([
['\u001b[32mGET\u001b[39m', ' ', '/api/v1/test'],
['\u001b[33mPOST\u001b[39m', ' ', '/api/v1/~/user'], // FIXME express5 we can't get the router path for nested
['\u001b[32mGET\u001b[39m', ' ', '/api/v1/~/user'], // FIXME express5 we can't get the router path for nested
['\u001b[34mPUT\u001b[39m', ' ', '/api/v1/~/user'], // FIXME express5 we can't get the router path for nested
]);
});
it('works with options', () => {
const logger = jest.fn();
const app = express5();
app.get('/user', handler);
app.post('/user', handler);
expressListRoutes(app, { logger, prefix: '/api/v1', spacer: 3 });
expect(logger.mock.calls).toEqual([
['\u001b[32mGET\u001b[39m', '', '/api/v1/user'],
['\u001b[33mPOST\u001b[39m', '', '/api/v1/user'],
]);
});
});
describe('unknown app', () => {
it('works if the app passed in does not have stacks/routes', () => {
const logger = jest.fn();
const paths = expressListRoutes({}, { logger });
expect(paths).toEqual([]);
expect(logger.mock.calls).toEqual([]);
});
it('handles "other" methods', () => {
const logger = jest.fn();
const app = express3();
app.trace('/test', handler);
const paths = expressListRoutes(app, { logger });
expect(paths).toEqual([{ method: 'TRACE', path: '/test' }]);
expect(logger.mock.calls).toEqual([['TRACE', ' ', '/test']]);
});
it('disable coloring method names if option is false', () => {
const logger = jest.fn();
const app = express3();
app.get('/test', handler);
const paths = expressListRoutes(app, { logger, color: false });
expect(paths).toEqual([{ method: 'GET', path: '/test' }]);
expect(logger.mock.calls).toEqual([['GET', ' ', '/test']]);
});
});