Skip to content

Commit 8fe559a

Browse files
committed
test: update test harness to use uws
1 parent aef03d6 commit 8fe559a

7 files changed

+69
-63
lines changed

jest.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export default async (): Promise<Config.InitialOptions> => ({
88
testEnvironment: "node",
99
testRunner: "jest-circus/runner",
1010
testMatch: ["<rootDir>/tests/**/*.test.ts"],
11-
globalSetup: "<rootDir>/tests/global-setup.ts",
12-
globalTeardown: "<rootDir>/tests/global-teardown.ts",
11+
setupFilesAfterEnv: ["<rootDir>/tests/setup.ts"],
1312
globals: {
1413
"ts-jest": {
1514
tsconfig: "<rootDir>/tests/tsconfig.json"

tests/PetitioRequest.test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
// eslint-disable-next-line node/no-extraneous-import
1+
import * as qs from "querystring";
22
import { Agent, Client } from "undici";
33
import type { HTTPMethod, TimeoutOptions } from "../src/lib/PetitioRequest";
44
import AbortController from "node-abort-controller";
55
import { URL as NURL } from "url";
66
import { PetitioRequest } from "../src/lib/PetitioRequest";
77
import { Readable } from "stream";
8-
import qs from "querystring";
98

10-
function url(host = "localhost:8080", method = "http") {
9+
const PORT = 8080 + Number(process.env.JEST_WORKER_ID);
10+
11+
function url(host = `localhost:${PORT}`, method = "http") {
1112
return `${method}://${host}`;
1213
}
1314

@@ -16,7 +17,7 @@ describe("Constructor", () => {
1617
expect.assertions(2);
1718

1819
function func() {
19-
return new PetitioRequest(url("localhost:8080", "wss"));
20+
return new PetitioRequest(url(`localhost:${PORT}`, "wss"));
2021
}
2122

2223
expect(func).toThrow("Bad URL protocol: wss:");
@@ -170,7 +171,7 @@ describe("Query Parameters", () => {
170171
test("GIVEN query param pair THEN transmit query", async () => {
171172
expect.assertions(1);
172173

173-
const request = new PetitioRequest(url("localhost:8080/get-echo"));
174+
const request = new PetitioRequest(url(`localhost:${PORT}/get-echo`));
174175
const response = await request
175176
.query("test", "test1")
176177
.query("test1", "test")
@@ -182,7 +183,7 @@ describe("Query Parameters", () => {
182183
test("GIVEN query params object THEN transmit query", async () => {
183184
expect.assertions(1);
184185

185-
const request = new PetitioRequest(url("localhost:8080/get-echo"));
186+
const request = new PetitioRequest(url(`localhost:${PORT}/get-echo`));
186187
const response = await request
187188
.query(OQS)
188189
.json<{ args: Record<string, string> }>();
@@ -228,7 +229,7 @@ describe("URL", () => {
228229
expect.assertions(1);
229230

230231
const path = "test";
231-
const req = new PetitioRequest(url(`localhost:8080/${path}`));
232+
const req = new PetitioRequest(url(`localhost:${PORT}/${path}`));
232233

233234
expect(req.url.pathname).toEqual(`/${path}`);
234235
});
@@ -292,7 +293,7 @@ describe("Sending", () => {
292293
test("GIVEN passed json THEN MATCH received raw", async () => {
293294
expect.assertions(1);
294295

295-
const response = await new PetitioRequest(url("localhost:8080/get-echo"))
296+
const response = await new PetitioRequest(url(`localhost:${PORT}/get-echo`))
296297
.query("test", "test1")
297298
.query("test1", "test")
298299
.raw();
@@ -303,7 +304,7 @@ describe("Sending", () => {
303304
test("GIVEN passed json THEN MATCH received text", async () => {
304305
expect.assertions(1);
305306

306-
const response = await new PetitioRequest("http://localhost:8080/get-echo")
307+
const response = await new PetitioRequest(`http://localhost:${PORT}/get-echo`)
307308
.query("test", "test1")
308309
.query("test1", "test")
309310
.text();
@@ -315,7 +316,7 @@ describe("Sending", () => {
315316
test("GIVEN passed json THEN MATCH received text", async () => {
316317
expect.assertions(1);
317318

318-
const res = await new PetitioRequest("http://localhost:8080/get-echo")
319+
const res = await new PetitioRequest(`http://localhost:${PORT}/get-echo`)
319320
.query("test", "test1")
320321
.query("test1", "test")
321322
.json();

tests/PetitioResponse.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { PetitioRequest } from "../src/lib/PetitioRequest";
22
import { PetitioResponse } from "../src/lib/PetitioResponse";
33

4-
function url(host = "localhost:8080", method = "http") {
4+
const PORT = 8080 + Number(process.env.JEST_WORKER_ID);
5+
6+
function url(host = `localhost:${PORT}`, method = "http") {
57
return `${method}://${host}`;
68
}
79

@@ -14,7 +16,7 @@ describe("Response Parsing", () => {
1416
title: "in collaboration we trust",
1517
body: "melius simul quam solus"
1618
};
17-
const request = new PetitioRequest(url("localhost:8080/json-test"));
19+
const request = new PetitioRequest(url(`localhost:${PORT}/json-test`));
1820
const response = await request.json();
1921

2022
expect(response).toEqual(json);

tests/global-setup.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/global-teardown.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/setup.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {
2+
App as createServer,
3+
us_listen_socket as uSocket,
4+
us_listen_socket_close as uSocketClose
5+
} from "uWebSockets.js";
6+
import { URL } from "url";
7+
8+
const JSON_RESPONSE = JSON.stringify({
9+
id: 0,
10+
title: "in collaboration we trust",
11+
body: "melius simul quam solus"
12+
});
13+
14+
const socks: uSocket[] = [];
15+
16+
const server = createServer()
17+
.get("/get-echo", (res, req) => {
18+
const headers: [string, string][] = [];
19+
let query = req.getQuery();
20+
query = query ? `?${query}` : "";
21+
const url = new URL(`http://localhost:8080${req.getUrl()}${query}`);
22+
23+
req.forEach((key, val) => {
24+
headers.push([key, val]);
25+
res.writeHeader(key, val);
26+
});
27+
28+
res.end(JSON.stringify({
29+
args: Object.fromEntries(url.searchParams.entries()),
30+
headers,
31+
url: url.href
32+
}));
33+
})
34+
.get("/json-test", (res) => {
35+
res.writeHeader("Content-Type", "application/json");
36+
res.end(JSON_RESPONSE);
37+
})
38+
.get("/*", (res) => {
39+
res.end("No command specified");
40+
});
41+
42+
beforeAll(() => {
43+
server.listen(
44+
"127.0.0.1",
45+
8080 + Number(process.env.JEST_WORKER_ID),
46+
(sock) => socks.push(sock)
47+
);
48+
});
49+
50+
afterAll(() => {
51+
for (const _i of socks) uSocketClose(socks.shift() as uSocket);
52+
});

tsconfig.eslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "./tsconfig.json",
3-
"includes": ["src", "tests", "*.config.*", "docs"]
3+
"include": ["src", "tests", "*.config.*", "docs"]
44
}

0 commit comments

Comments
 (0)