Skip to content

Commit 00b8dfb

Browse files
authored
test(nodejs-hybrid-app): factor test setup (#7628)
1 parent 755a27c commit 00b8dfb

File tree

1 file changed

+42
-67
lines changed

1 file changed

+42
-67
lines changed
Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,67 @@
11
import { resolve } from "node:path";
22
import { fetch } from "undici";
3-
import { describe, it, test } from "vitest";
3+
import { afterAll, beforeAll, describe, it, test } from "vitest";
44
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";
55

66
describe("nodejs compat", () => {
7+
let wrangler: Awaited<ReturnType<typeof runWranglerDev>>;
8+
9+
beforeAll(async () => {
10+
wrangler = await runWranglerDev(resolve(__dirname, "../src"), [
11+
"--port=0",
12+
"--inspector-port=0",
13+
]);
14+
});
15+
16+
afterAll(async () => {
17+
await wrangler.stop();
18+
});
719
it("should work when running code requiring polyfills", async ({
820
expect,
921
}) => {
10-
const { ip, port, stop } = await runWranglerDev(
11-
resolve(__dirname, "../src"),
12-
["--port=0", "--inspector-port=0"]
13-
);
14-
try {
15-
const response = await fetch(`http://${ip}:${port}/test-process`);
16-
const body = await response.text();
17-
expect(body).toMatchInlineSnapshot(`"OK!"`);
22+
const { ip, port } = wrangler;
23+
const response = await fetch(`http://${ip}:${port}/test-process`);
24+
const body = await response.text();
25+
expect(body).toMatchInlineSnapshot(`"OK!"`);
1826

19-
// Disabling actually querying the database since we are getting this error:
20-
// > too many connections for role 'reader'
21-
// const response = await fetch(`http://${ip}:${port}/query`);
22-
// const body = await response.text();
23-
// console.log(body);
24-
// const result = JSON.parse(body) as { id: string };
25-
// expect(result.id).toEqual("1");
26-
} finally {
27-
await stop();
28-
}
27+
// Disabling actually querying the database since we are getting this error:
28+
// > too many connections for role 'reader'
29+
// const response = await fetch(`http://${ip}:${port}/query`);
30+
// const body = await response.text();
31+
// console.log(body);
32+
// const result = JSON.parse(body) as { id: string };
33+
// expect(result.id).toEqual("1");
2934
});
3035

3136
it("should be able to call `getRandomValues()` bound to any object", async ({
3237
expect,
3338
}) => {
34-
const { ip, port, stop } = await runWranglerDev(
35-
resolve(__dirname, "../src"),
36-
["--port=0", "--inspector-port=0"]
37-
);
38-
try {
39-
const response = await fetch(`http://${ip}:${port}/test-random`);
40-
const body = await response.json();
41-
expect(body).toEqual([
42-
expect.any(String),
43-
expect.any(String),
44-
expect.any(String),
45-
expect.any(String),
46-
]);
47-
} finally {
48-
await stop();
49-
}
39+
const { ip, port } = wrangler;
40+
const response = await fetch(`http://${ip}:${port}/test-random`);
41+
const body = await response.json();
42+
expect(body).toEqual([
43+
expect.any(String),
44+
expect.any(String),
45+
expect.any(String),
46+
expect.any(String),
47+
]);
5048
});
5149

5250
test("crypto.X509Certificate is implemented", async ({ expect }) => {
53-
const { ip, port, stop } = await runWranglerDev(
54-
resolve(__dirname, "../src"),
55-
["--port=0", "--inspector-port=0"]
56-
);
57-
try {
58-
const response = await fetch(
59-
`http://${ip}:${port}/test-x509-certificate`
60-
);
61-
await expect(response.text()).resolves.toBe(`"OK!"`);
62-
} finally {
63-
await stop();
64-
}
51+
const { ip, port } = wrangler;
52+
const response = await fetch(`http://${ip}:${port}/test-x509-certificate`);
53+
await expect(response.text()).resolves.toBe(`"OK!"`);
6554
});
6655

6756
test("import unenv aliased packages", async ({ expect }) => {
68-
const { ip, port, stop } = await runWranglerDev(
69-
resolve(__dirname, "../src"),
70-
["--port=0", "--inspector-port=0"]
71-
);
72-
try {
73-
const response = await fetch(`http://${ip}:${port}/test-require-alias`);
74-
await expect(response.text()).resolves.toBe(`"OK!"`);
75-
} finally {
76-
await stop();
77-
}
57+
const { ip, port } = wrangler;
58+
const response = await fetch(`http://${ip}:${port}/test-require-alias`);
59+
await expect(response.text()).resolves.toBe(`"OK!"`);
7860
});
7961

8062
test("unenv preset", async ({ expect }) => {
81-
const { ip, port, stop } = await runWranglerDev(
82-
resolve(__dirname, "../src"),
83-
["--port=0", "--inspector-port=0"]
84-
);
85-
try {
86-
const response = await fetch(`http://${ip}:${port}/test-unenv-preset`);
87-
await expect(response.text()).resolves.toBe("OK!");
88-
} finally {
89-
await stop();
90-
}
63+
const { ip, port } = wrangler;
64+
const response = await fetch(`http://${ip}:${port}/test-unenv-preset`);
65+
await expect(response.text()).resolves.toBe("OK!");
9166
});
9267
});

0 commit comments

Comments
 (0)