deno.land / x / abc@v1.3.3 / examples / test.ts

نووسراو ببینە
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
71
72
73
74
import { join } from "../vendor/https/deno.land/std/path/mod.ts";import { assert, assertEquals,} from "../vendor/https/deno.land/std/testing/asserts.ts";import { BufReader } from "../vendor/https/deno.land/std/io/bufio.ts";import { TextProtoReader } from "../vendor/https/deno.land/std/textproto/mod.ts";const { run, test, execPath, chdir, cwd } = Deno;
const dir = join(import.meta.url, "..");const addr = "http://localhost:8080";let server: Deno.Process<Deno.RunOptions & { stdout: "piped" }>;
async function startServer(fpath: string): Promise<void> { server = run({ cmd: [execPath(), "run", "--allow-net", "--allow-read", fpath], stdout: "piped", }); assert(server.stdout != null);
const r = new TextProtoReader(new BufReader(server.stdout)); const s = await r.readLine(); assert(s !== null && s.includes("server listening"));}
function killServer(): void { server.close(); server.stdout?.close();}
test("exmaples cat app", async function () { await startServer(join(dir, "./cat_app/main.ts")); try { const cat = { name: "zhmushan", age: 18 }; const createdCat = await fetch(addr, { method: "POST", body: JSON.stringify(cat), headers: { "content-type": "application/json", }, }).then((resp) => resp.json()); const foundCats = await fetch(addr).then((resp) => resp.json()); const foundCat = await fetch(`${addr}/1`).then((resp) => resp.json());
assertEquals(createdCat, { id: 1, ...cat }); assertEquals(foundCat, createdCat); assertEquals(foundCats, [foundCat]); } finally { killServer(); }});
test("exmaples jsx", async function () { await startServer(join(dir, "./jsx/main.jsx")); try { const text = await fetch(addr).then((resp) => resp.text()); assertEquals(text, `<h1 data-reactroot="">Hello</h1>`); } finally { killServer(); }});
test("exmaples template", async function () { chdir(join(cwd(), "./examples/template")); await startServer(join(dir, "./template/main.ts")); try { const text = await fetch(addr).then((resp) => resp.text()); assert(text.includes("hello, zhmushan!")); } finally { killServer(); chdir("../../"); }});
abc

Version Info

Tagged at
2 years ago