-
Notifications
You must be signed in to change notification settings - Fork 879
Expand file tree
/
Copy pathexpress-error-middleware.spec.ts
More file actions
27 lines (21 loc) · 951 Bytes
/
Copy pathexpress-error-middleware.spec.ts
File metadata and controls
27 lines (21 loc) · 951 Bytes
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
import type express from 'express';
import request from 'supertest';
import { describe, expect, it } from 'vitest';
import { createApp, createProxyMiddleware } from './test-kit.js';
describe('express error middleware', () => {
it('should propagate error to express', async () => {
let httpProxyError: Error | undefined;
const proxyMiddleware = createProxyMiddleware({
changeOrigin: true,
router: (req) => undefined, // Trigger "Error: Must provide a proper URL as target"
});
const errorMiddleware: express.ErrorRequestHandler = (err, req, res, next) => {
httpProxyError = err;
res.status(504).send('Something broke!');
};
const app = createApp(proxyMiddleware, errorMiddleware as any);
const response = await request(app).get('/get').expect(504);
expect(httpProxyError?.message).toBe('Must provide a proper URL as target');
expect(response.text).toBe('Something broke!');
});
});