Skip to content

Commit 88548fc

Browse files
authored
refactor(test): fix flakiness (#616)
1 parent 21c70a0 commit 88548fc

File tree

5 files changed

+44
-41
lines changed

5 files changed

+44
-41
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"eslint-config-prettier": "^8.3.0",
7070
"eslint-plugin-prettier": "^3.4.0",
7171
"express": "^4.17.1",
72+
"get-port": "^5.1.1",
7273
"husky": "^6.0.0",
7374
"jest": "^26.6.3",
7475
"lint-staged": "^11.0.0",

test/e2e/express-router.spec.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import * as express from 'express';
2-
import * as http from 'http';
2+
import * as request from 'supertest';
33
import { createProxyMiddleware } from './test-kit';
44
import { Options } from '../../src/index';
55

66
describe('Usage in Express', () => {
77
let app: express.Express;
8-
let server: http.Server;
8+
let agent: request.SuperTest<request.Test>;
99

1010
beforeEach(() => {
1111
app = express();
1212
});
1313

14-
afterEach(() => {
15-
server?.close();
16-
});
17-
1814
// https://github.com/chimurai/http-proxy-middleware/issues/94
1915
describe('Express Sub Route', () => {
2016
beforeEach(() => {
@@ -43,19 +39,12 @@ describe('Usage in Express', () => {
4339
app.use('/sub', sub);
4440

4541
// start server
46-
server = app.listen(3000);
42+
agent = request(app);
4743
});
4844

49-
// FIXME: flaky e2e test; caused by fixed port:3000
50-
it('should still return a response when route does not match proxyConfig', (done) => {
51-
let responseBody;
52-
http.get('http://localhost:3000/sub/hello', (res) => {
53-
res.on('data', (chunk) => {
54-
responseBody = chunk.toString();
55-
expect(responseBody).toBe('{"content":"foobar"}');
56-
done();
57-
});
58-
});
45+
it('should still return a response when route does not match proxyConfig', async () => {
46+
const response = await agent.get('/sub/hello');
47+
expect(response.body).toEqual({ content: 'foobar' });
5948
});
6049
});
6150

test/e2e/router.spec.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createProxyMiddleware, createApp, createAppWithPath } from './test-kit'
22
import { ErrorRequestHandler } from 'express';
33
import * as request from 'supertest';
44
import { getLocal, generateCACertificate, Mockttp } from 'mockttp';
5+
import * as getPort from 'get-port';
56

67
const untrustedCACert = generateCACertificate({ bits: 1024 });
78

@@ -12,11 +13,19 @@ describe('E2E router', () => {
1213
let targetServerB: Mockttp;
1314
let targetServerC: Mockttp;
1415

16+
let targetPortA: number;
17+
let targetPortB: number;
18+
let targetPortC: number;
19+
1520
beforeEach(async () => {
1621
targetServerA = getLocal({ https: await untrustedCACert });
1722
targetServerB = getLocal({ https: await untrustedCACert });
1823
targetServerC = getLocal({ https: await untrustedCACert });
1924

25+
targetPortA = await getPort();
26+
targetPortB = await getPort();
27+
targetPortC = await getPort();
28+
2029
await targetServerA
2130
.anyRequest()
2231
.thenPassThrough({ ignoreHostCertificateErrors: ['localhost'] });
@@ -37,9 +46,9 @@ describe('E2E router', () => {
3746
.anyRequest()
3847
.thenCallback(({ protocol }) => ({ body: protocol === 'https' ? 'C' : 'NOT HTTPS C' }));
3948

40-
await targetServerA.start(6001);
41-
await targetServerB.start(6002);
42-
await targetServerC.start(6003);
49+
await targetServerA.start(targetPortA);
50+
await targetServerB.start(targetPortB);
51+
await targetServerC.start(targetPortC);
4352
});
4453

4554
afterEach(async () => {
@@ -52,11 +61,11 @@ describe('E2E router', () => {
5261
it('should work with a string', async () => {
5362
const app = createApp(
5463
createProxyMiddleware({
55-
target: 'https://localhost:6001',
64+
target: `https://localhost:${targetPortA}`,
5665
secure: false,
5766
changeOrigin: true,
5867
router(req) {
59-
return 'https://localhost:6003';
68+
return `https://localhost:${targetPortC}`;
6069
},
6170
})
6271
);
@@ -69,11 +78,11 @@ describe('E2E router', () => {
6978
it('should work with an object', async () => {
7079
const app = createApp(
7180
createProxyMiddleware({
72-
target: 'https://localhost:6001',
81+
target: `https://localhost:${targetPortA}`,
7382
secure: false,
7483
changeOrigin: true,
7584
router(req) {
76-
return { host: 'localhost', port: 6003, protocol: 'https:' };
85+
return { host: 'localhost', port: targetPortC, protocol: 'https:' };
7786
},
7887
})
7988
);
@@ -85,12 +94,12 @@ describe('E2E router', () => {
8594
it('should work with an async callback', async () => {
8695
const app = createApp(
8796
createProxyMiddleware({
88-
target: 'https://localhost:6001',
97+
target: `https://localhost:${targetPortA}`,
8998
secure: false,
9099
changeOrigin: true,
91100
router: async (req) => {
92101
return new Promise((resolve) =>
93-
resolve({ host: 'localhost', port: 6003, protocol: 'https:' })
102+
resolve({ host: 'localhost', port: targetPortC, protocol: 'https:' })
94103
);
95104
},
96105
})
@@ -104,7 +113,7 @@ describe('E2E router', () => {
104113
it('should handle promise rejection in router', async () => {
105114
const app = createApp(
106115
createProxyMiddleware({
107-
target: 'https://localhost:6001',
116+
target: `https://localhost:${targetPortA}`,
108117
secure: false,
109118
changeOrigin: true,
110119
router: async (req) => {
@@ -125,12 +134,12 @@ describe('E2E router', () => {
125134
it('missing a : will cause it to use http', async () => {
126135
const app = createApp(
127136
createProxyMiddleware({
128-
target: 'https://localhost:6001',
137+
target: `https://localhost:${targetPortA}`,
129138
secure: false,
130139
changeOrigin: true,
131140
router: async (req) => {
132141
return new Promise((resolve) =>
133-
resolve({ host: 'localhost', port: 6003, protocol: 'https' })
142+
resolve({ host: 'localhost', port: targetPortC, protocol: 'https' })
134143
);
135144
},
136145
})
@@ -149,13 +158,13 @@ describe('E2E router', () => {
149158
const app = createAppWithPath(
150159
'/',
151160
createProxyMiddleware({
152-
target: 'https://localhost:6001',
161+
target: `https://localhost:${targetPortA}`,
153162
secure: false,
154163
changeOrigin: true,
155164
router: {
156-
'alpha.localhost:6000': 'https://localhost:6001',
157-
'beta.localhost:6000': 'https://localhost:6002',
158-
'localhost:6000/api': 'https://localhost:6003',
165+
'alpha.localhost:6000': `https://localhost:${targetPortA}`,
166+
'beta.localhost:6000': `https://localhost:${targetPortB}`,
167+
'localhost:6000/api': `https://localhost:${targetPortC}`,
159168
},
160169
})
161170
);

test/e2e/websocket.spec.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as http from 'http';
22
import * as WebSocket from 'ws';
33
import { Server as WebSocketServer } from 'ws';
4+
import * as getPort from 'get-port';
45
import { createProxyMiddleware, createApp } from './test-kit';
56
import type { RequestHandler } from '../../src/types';
67

@@ -14,9 +15,13 @@ describe('E2E WebSocket proxy', () => {
1415
let ws: WebSocket;
1516
let wss: WebSocketServer;
1617
let proxyMiddleware: RequestHandler;
17-
const WS_SERVER_PORT = 9000;
18+
let WS_SERVER_PORT: number;
19+
let SERVER_PORT: number;
20+
21+
beforeEach(async () => {
22+
WS_SERVER_PORT = await getPort();
23+
SERVER_PORT = await getPort();
1824

19-
beforeEach(() => {
2025
wss = new WebSocketServer({ port: WS_SERVER_PORT });
2126

2227
wss.on('connection', (websocket) => {
@@ -44,7 +49,6 @@ describe('E2E WebSocket proxy', () => {
4449

4550
describe('option.ws', () => {
4651
beforeEach(async (done) => {
47-
const SERVER_PORT = 31000;
4852
proxyServer = createApp(proxyMiddleware).listen(SERVER_PORT);
4953

5054
// quick & dirty Promise version of http.get (don't care about correctness)
@@ -70,7 +74,6 @@ describe('E2E WebSocket proxy', () => {
7074

7175
describe('option.ws with external server "upgrade"', () => {
7276
beforeEach((done) => {
73-
const SERVER_PORT = 32000;
7477
proxyServer = createApp(proxyMiddleware).listen(SERVER_PORT);
7578
proxyServer.on('upgrade', proxyMiddleware.upgrade);
7679

@@ -88,8 +91,6 @@ describe('E2E WebSocket proxy', () => {
8891
});
8992

9093
describe('option.ws with external server "upgrade" and shorthand usage', () => {
91-
const SERVER_PORT = 33000;
92-
9394
beforeEach(() => {
9495
proxyServer = createApp(
9596
createProxyMiddleware(`ws://localhost:${WS_SERVER_PORT}`, {
@@ -115,8 +116,6 @@ describe('E2E WebSocket proxy', () => {
115116
});
116117

117118
describe('with router and pathRewrite', () => {
118-
const SERVER_PORT = 34000;
119-
120119
beforeEach(() => {
121120
// override
122121
proxyServer = createApp(

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3040,6 +3040,11 @@ get-own-enumerable-property-symbols@^3.0.0:
30403040
resolved "https://registry.yarnpkg.com/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-3.0.2.tgz#b5fde77f22cbe35f390b4e089922c50bce6ef664"
30413041
integrity sha512-I0UBV/XOz1XkIJHEUDMZAbzCThU/H8DxmSfmdGcKPnVhu2VfFqr34jr9777IyaTYvxjedWhqVIilEDsCdP5G6g==
30423042

3043+
get-port@^5.1.1:
3044+
version "5.1.1"
3045+
resolved "https://registry.yarnpkg.com/get-port/-/get-port-5.1.1.tgz#0469ed07563479de6efb986baf053dcd7d4e3193"
3046+
integrity sha512-g/Q1aTSDOxFpchXC4i8ZWvxA1lnPqx/JHqcpIw0/LX9T8x/GBbi6YnlN5nhaKIFkT8oFsscUKgDJYxfwfS6QsQ==
3047+
30433048
30443049
version "8.0.0"
30453050
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-8.0.0.tgz#cbad6a73feb75f6eeb22ba9e01f89aa28aa97a53"

0 commit comments

Comments
 (0)