Skip to content

Commit 95b22e3

Browse files
committed
Use async-listen to replace the test utility function listen.
1 parent 629480b commit 95b22e3

7 files changed

+311
-301
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Migrated to the ESLint v9 CLI and “flat” config.
2323
- Removed the Node.js CLI option `--unhandled-rejections=throw` in the package script `tests` as it’s now the default for all supported Node.js versions.
2424
- Omit unused catch bindings in the function `processRequest`.
25+
- Added a new dev dependency [`async-listen`](https://npm.im/async-listen) to replace the test utility function `listen`.
2526

2627
## 16.0.2
2728

graphqlUploadExpress.test.mjs

Lines changed: 123 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { deepStrictEqual, ok, strictEqual } from "node:assert";
66
import { createServer } from "node:http";
77
import { describe, it } from "node:test";
88

9+
import { listen } from "async-listen";
910
import express from "express";
1011
import createError from "http-errors";
1112

1213
import graphqlUploadExpress from "./graphqlUploadExpress.mjs";
1314
import processRequest from "./processRequest.mjs";
14-
import listen from "./test/listen.mjs";
1515

1616
describe(
1717
"Function `graphqlUploadExpress`.",
@@ -22,22 +22,23 @@ describe(
2222
it("Non multipart request.", async () => {
2323
let processRequestRan = false;
2424

25-
const app = express().use(
26-
graphqlUploadExpress({
27-
/** @type {any} */
28-
async processRequest() {
29-
processRequestRan = true;
30-
},
31-
}),
25+
const server = createServer(
26+
express().use(
27+
graphqlUploadExpress({
28+
/** @type {any} */
29+
async processRequest() {
30+
processRequestRan = true;
31+
},
32+
}),
33+
),
3234
);
33-
34-
const { port, close } = await listen(createServer(app));
35+
const url = await listen(server);
3536

3637
try {
37-
await fetch(`http://localhost:${port}`, { method: "POST" });
38+
await fetch(url, { method: "POST" });
3839
strictEqual(processRequestRan, false);
3940
} finally {
40-
close();
41+
server.close();
4142
}
4243
});
4344

@@ -51,14 +52,15 @@ describe(
5152
*/
5253
let requestBody;
5354

54-
const app = express()
55-
.use(graphqlUploadExpress())
56-
.use((request, response, next) => {
57-
requestBody = request.body;
58-
next();
59-
});
60-
61-
const { port, close } = await listen(createServer(app));
55+
const server = createServer(
56+
express()
57+
.use(graphqlUploadExpress())
58+
.use((request, response, next) => {
59+
requestBody = request.body;
60+
next();
61+
}),
62+
);
63+
const url = await listen(server);
6264

6365
try {
6466
const body = new FormData();
@@ -70,13 +72,13 @@ describe(
7072
body.append("map", JSON.stringify({ 1: ["variables.file"] }));
7173
body.append("1", new File(["a"], "a.txt", { type: "text/plain" }));
7274

73-
await fetch(`http://localhost:${port}`, { method: "POST", body });
75+
await fetch(url, { method: "POST", body });
7476

7577
ok(requestBody);
7678
ok(requestBody.variables);
7779
ok(requestBody.variables.file);
7880
} finally {
79-
close();
81+
server.close();
8082
}
8183
});
8284

@@ -92,21 +94,23 @@ describe(
9294
*/
9395
let requestBody;
9496

95-
const app = express()
96-
.use(
97-
graphqlUploadExpress({
98-
processRequest(...args) {
99-
processRequestRan = true;
100-
return processRequest(...args);
101-
},
97+
const server = createServer(
98+
express()
99+
.use(
100+
graphqlUploadExpress({
101+
processRequest(...args) {
102+
processRequestRan = true;
103+
return processRequest(...args);
104+
},
105+
}),
106+
)
107+
.use((request, response, next) => {
108+
requestBody = request.body;
109+
next();
102110
}),
103-
)
104-
.use((request, response, next) => {
105-
requestBody = request.body;
106-
next();
107-
});
111+
);
108112

109-
const { port, close } = await listen(createServer(app));
113+
const url = await listen(server);
110114

111115
try {
112116
const body = new FormData();
@@ -118,14 +122,14 @@ describe(
118122
body.append("map", JSON.stringify({ 1: ["variables.file"] }));
119123
body.append("1", new File(["a"], "a.txt", { type: "text/plain" }));
120124

121-
await fetch(`http://localhost:${port}`, { method: "POST", body });
125+
await fetch(url, { method: "POST", body });
122126

123127
strictEqual(processRequestRan, true);
124128
ok(requestBody);
125129
ok(requestBody.variables);
126130
ok(requestBody.variables.file);
127131
} finally {
128-
close();
132+
server.close();
129133
}
130134
});
131135

@@ -135,47 +139,49 @@ describe(
135139
let responseStatusCode;
136140

137141
const error = createError(400, "Message.");
138-
const app = express()
139-
.use((request, response, next) => {
140-
const { send } = response;
141-
142-
// @ts-ignore Todo: Find a less hacky way.
143-
response.send = (...args) => {
144-
requestCompleted = request.complete;
145-
response.send = send;
146-
response.send(...args);
147-
};
148-
149-
next();
150-
})
151-
.use(
152-
graphqlUploadExpress({
153-
async processRequest(request) {
154-
request.resume();
155-
throw error;
142+
const server = createServer(
143+
express()
144+
.use((request, response, next) => {
145+
const { send } = response;
146+
147+
// @ts-ignore Todo: Find a less hacky way.
148+
response.send = (...args) => {
149+
requestCompleted = request.complete;
150+
response.send = send;
151+
response.send(...args);
152+
};
153+
154+
next();
155+
})
156+
.use(
157+
graphqlUploadExpress({
158+
async processRequest(request) {
159+
request.resume();
160+
throw error;
161+
},
162+
}),
163+
)
164+
.use(
165+
/**
166+
* @param {Error} error
167+
* @param {import("express").Request} request
168+
* @param {import("express").Response} response
169+
* @param {import("express").NextFunction} next
170+
*/
171+
(error, request, response, next) => {
172+
expressError = error;
173+
responseStatusCode = response.statusCode;
174+
175+
// Sending a response here prevents the default Express error
176+
// handler from running, which would undesirably (in this case)
177+
// display the error in the console.
178+
if (response.headersSent) next(error);
179+
else response.send();
156180
},
157-
}),
158-
)
159-
.use(
160-
/**
161-
* @param {Error} error
162-
* @param {import("express").Request} request
163-
* @param {import("express").Response} response
164-
* @param {import("express").NextFunction} next
165-
*/
166-
(error, request, response, next) => {
167-
expressError = error;
168-
responseStatusCode = response.statusCode;
169-
170-
// Sending a response here prevents the default Express error handler
171-
// from running, which would undesirably (in this case) display the
172-
// error in the console.
173-
if (response.headersSent) next(error);
174-
else response.send();
175-
},
176-
);
181+
),
182+
);
177183

178-
const { port, close } = await listen(createServer(app));
184+
const url = await listen(server);
179185

180186
try {
181187
const body = new FormData();
@@ -187,7 +193,7 @@ describe(
187193
body.append("map", JSON.stringify({ 1: ["variables.file"] }));
188194
body.append("1", new File(["a"], "a.txt", { type: "text/plain" }));
189195

190-
await fetch(`http://localhost:${port}`, { method: "POST", body });
196+
await fetch(url, { method: "POST", body });
191197

192198
deepStrictEqual(expressError, error);
193199
ok(
@@ -196,7 +202,7 @@ describe(
196202
);
197203
strictEqual(responseStatusCode, error.status);
198204
} finally {
199-
close();
205+
server.close();
200206
}
201207
});
202208

@@ -205,42 +211,44 @@ describe(
205211
let requestCompleted;
206212

207213
const error = new Error("Message.");
208-
const app = express()
209-
.use((request, response, next) => {
210-
const { send } = response;
211-
212-
// @ts-ignore Todo: Find a less hacky way.
213-
response.send = (...args) => {
214-
requestCompleted = request.complete;
215-
response.send = send;
216-
response.send(...args);
217-
};
218-
219-
next();
220-
})
221-
.use(graphqlUploadExpress())
222-
.use(() => {
223-
throw error;
224-
})
225-
.use(
226-
/**
227-
* @param {Error} error
228-
* @param {import("express").Request} request
229-
* @param {import("express").Response} response
230-
* @param {import("express").NextFunction} next
231-
*/
232-
(error, request, response, next) => {
233-
expressError = error;
234-
235-
// Sending a response here prevents the default Express error handler
236-
// from running, which would undesirably (in this case) display the
237-
// error in the console.
238-
if (response.headersSent) next(error);
239-
else response.send();
240-
},
241-
);
214+
const server = createServer(
215+
express()
216+
.use((request, response, next) => {
217+
const { send } = response;
218+
219+
// @ts-ignore Todo: Find a less hacky way.
220+
response.send = (...args) => {
221+
requestCompleted = request.complete;
222+
response.send = send;
223+
response.send(...args);
224+
};
225+
226+
next();
227+
})
228+
.use(graphqlUploadExpress())
229+
.use(() => {
230+
throw error;
231+
})
232+
.use(
233+
/**
234+
* @param {Error} error
235+
* @param {import("express").Request} request
236+
* @param {import("express").Response} response
237+
* @param {import("express").NextFunction} next
238+
*/
239+
(error, request, response, next) => {
240+
expressError = error;
241+
242+
// Sending a response here prevents the default Express error
243+
// handler from running, which would undesirably (in this case)
244+
// display the error in the console.
245+
if (response.headersSent) next(error);
246+
else response.send();
247+
},
248+
),
249+
);
242250

243-
const { port, close } = await listen(createServer(app));
251+
const url = await listen(server);
244252

245253
try {
246254
const body = new FormData();
@@ -252,15 +260,15 @@ describe(
252260
body.append("map", JSON.stringify({ 1: ["variables.file"] }));
253261
body.append("1", new File(["a"], "a.txt", { type: "text/plain" }));
254262

255-
await fetch(`http://localhost:${port}`, { method: "POST", body });
263+
await fetch(url, { method: "POST", body });
256264

257265
deepStrictEqual(expressError, error);
258266
ok(
259267
requestCompleted,
260268
"Response wasn’t delayed until the request completed.",
261269
);
262270
} finally {
263-
close();
271+
server.close();
264272
}
265273
});
266274
},

0 commit comments

Comments
 (0)