-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
70 lines (59 loc) · 2.11 KB
/
test.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import * as assert from "node:assert/strict";
import * as path from "node:path";
import { test } from "node:test";
import { fileURLToPath } from "node:url";
import { createServer } from "vite";
async function spawnDevServer(dir) {
let devServer = await createServer({
root: path.resolve(
path.join(path.dirname(fileURLToPath(import.meta.url)), "examples"),
dir
),
});
return {
fetch(env, path = "/", init, entry = "src/index.ts") {
return devServer.environments[env].dispatchFetch(
entry,
new Request(`http://localhost${path}`, init)
);
},
async close() {
await devServer.close();
},
};
}
test("basic", async (t) => {
const server = await spawnDevServer("basic");
t.after(async () => {
await server.close();
});
assert.strictEqual(await (await server.fetch("ssr")).text(), "Hello, World!");
});
test("durable-object", async (t) => {
const server = await spawnDevServer("durable-object");
t.after(async () => {
await server.close();
});
const initialHtml = await (await server.fetch("ssr")).text();
const [, initialCountStr] = initialHtml.match(/Count: (\d+)/);
const initialCount = Number.parseInt(initialCountStr, 10);
await server.fetch("ssr", "/", { method: "POST" });
const resultHtml = await (await server.fetch("ssr")).text();
const [, resultCountStr] = resultHtml.match(/Count: (\d+)/);
const resultCount = Number.parseInt(resultCountStr, 10);
assert.strictEqual(initialCount + 1, resultCount);
});
test("multi-env", async (t) => {
const server = await spawnDevServer("multi-env");
t.after(async () => {
await server.close();
});
const initialHtml = await (await server.fetch("ssr")).text();
const [, initialCountStr] = initialHtml.match(/Count: (\d+)/);
const initialCount = Number.parseInt(initialCountStr, 10);
await server.fetch("ssr", "/", { method: "POST" });
const resultHtml = await (await server.fetch("ssr")).text();
const [, resultCountStr] = resultHtml.match(/Count: (\d+)/);
const resultCount = Number.parseInt(resultCountStr, 10);
assert.strictEqual(initialCount + 1, resultCount);
});