deno.land / x / fresh@1.1.1 / tests / cli_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388import { Status } from "../src/server/deps.ts";import { assert, assertEquals, assertStringIncludes, delay, puppeteer, TextLineStream,} from "./deps.ts";
type FileTree = { type: "file"; name: string;} | { type: "directory"; name: string; contents: FileTree[];};
const assertFileExistence = async (tree: FileTree[], dirname?: string) => { for (const t of tree) { const stat = await Deno.stat( dirname ? [dirname, t.name].join("/") : t.name, ); assertEquals(t.type === "file", stat.isFile);
if (t.type === "directory") { assert(stat.isDirectory); await assertFileExistence( t.contents, dirname ? [dirname, t.name].join("/") : t.name, ); } }};
Deno.test({ name: "fresh-init", async fn(t) { // Preparation const tmpDirName = await Deno.makeTempDir();
await t.step("execute init command", async () => { const cliProcess = Deno.run({ cmd: [ "deno", "run", "-A", "init.ts", tmpDirName, ], stdin: "null", stdout: "null", }); const { code } = await cliProcess.status(); cliProcess.close(); assertEquals(code, 0); });
// NOTE: generated by `tree -J <dir>` const targetFileTree: FileTree[] = [ { "type": "directory", "name": tmpDirName, "contents": [ { "type": "file", "name": "README.md" }, { "type": "file", "name": "import_map.json" }, { "type": "file", "name": "fresh.gen.ts" }, { "type": "directory", "name": "components", "contents": [ { "type": "file", "name": "Button.tsx" }, ], }, { "type": "directory", "name": "islands", "contents": [ { "type": "file", "name": "Counter.tsx" }, ], }, { "type": "file", "name": "main.ts" }, { "type": "directory", "name": "routes", "contents": [ { "type": "file", "name": "[name].tsx" }, { "type": "directory", "name": "api", "contents": [ { "type": "file", "name": "joke.ts" }, ], }, { "type": "file", "name": "index.tsx" }, ], }, { "type": "directory", "name": "static", "contents": [ { "type": "file", "name": "logo.svg" }, ], }, ], }, ];
await t.step("check generated files", async () => { await assertFileExistence(targetFileTree); });
await t.step("start up the server and access the root page", async () => { const serverProcess = Deno.run({ cmd: ["deno", "run", "-A", "--check", "main.ts"], stdin: "null", stdout: "piped", stderr: "inherit", cwd: tmpDirName, });
const lines = serverProcess.stdout.readable .pipeThrough(new TextDecoderStream()) .pipeThrough(new TextLineStream());
let started = false; for await (const line of lines) { console.log(line); if (line.includes("Listening on http://")) { started = true; break; } } if (!started) { throw new Error("Server didn't start up"); }
await delay(100);
// Access the root page const res = await fetch("http://localhost:8000"); await res.body?.cancel(); assertEquals(res.status, Status.OK);
// verify the island is revived. const browser = await puppeteer.launch({ args: ["--no-sandbox"], }); const page = await browser.newPage(); await page.goto("http://localhost:8000", { waitUntil: "networkidle2" }); const counter = await page.$("body > div > div > p"); let counterValue = await counter?.evaluate((el) => el.textContent); assert(counterValue === "3");
const buttonPlus = await page.$("body > div > div > button:nth-child(3)"); await buttonPlus?.click();
await delay(100);
counterValue = await counter?.evaluate((el) => el.textContent); assert(counterValue === "4"); await page.close(); await browser.close();
await lines.cancel(); serverProcess.kill("SIGTERM"); serverProcess.close(); }); }, sanitizeOps: false, sanitizeResources: false,});
Deno.test({ name: "fresh-init --twind --vscode", async fn(t) { // Preparation const tmpDirName = await Deno.makeTempDir();
await t.step("execute init command", async () => { const cliProcess = Deno.run({ cmd: [ "deno", "run", "-A", "init.ts", tmpDirName, "--twind", "--vscode", ], stdin: "null", stdout: "null", }); const { code } = await cliProcess.status(); cliProcess.close(); assertEquals(code, 0); });
// NOTE: generated by `tree -J <dir>` const targetFileTree: FileTree[] = [ { "type": "directory", "name": tmpDirName, "contents": [ { "type": "file", "name": "README.md" }, { "type": "file", "name": "import_map.json" }, { "type": "file", "name": "fresh.gen.ts" }, { "type": "file", "name": "twind.config.ts" }, { "type": "directory", "name": "components", "contents": [ { "type": "file", "name": "Button.tsx" }, ], }, { "type": "directory", "name": "islands", "contents": [ { "type": "file", "name": "Counter.tsx" }, ], }, { "type": "file", "name": "main.ts" }, { "type": "directory", "name": "routes", "contents": [ { "type": "file", "name": "[name].tsx" }, { "type": "directory", "name": "api", "contents": [ { "type": "file", "name": "joke.ts" }, ], }, { "type": "file", "name": "index.tsx" }, ], }, { "type": "directory", "name": "static", "contents": [ { "type": "file", "name": "logo.svg" }, ], }, { "type": "directory", "name": ".vscode", "contents": [ { "type": "file", "name": "settings.json" }, { "type": "file", "name": "extensions.json" }, ], }, ], }, ];
await t.step("check generated files", async () => { await assertFileExistence(targetFileTree); });
await t.step("start up the server and access the root page", async () => { const serverProcess = Deno.run({ cmd: ["deno", "run", "-A", "--check", "main.ts"], stdin: "null", stdout: "piped", stderr: "inherit", cwd: tmpDirName, });
const lines = serverProcess.stdout.readable .pipeThrough(new TextDecoderStream()) .pipeThrough(new TextLineStream());
let started = false; for await (const line of lines) { console.log(line); if (line.includes("Listening on http://")) { started = true; break; } } if (!started) { throw new Error("Server didn't start up"); }
await delay(100);
// Access the root page const res = await fetch("http://localhost:8000"); await res.body?.cancel(); assertEquals(res.status, Status.OK);
// verify the island is revived. const browser = await puppeteer.launch({ args: ["--no-sandbox"] }); const page = await browser.newPage(); await page.goto("http://localhost:8000", { waitUntil: "networkidle2" });
const counter = await page.$("body > div > div > p"); let counterValue = await counter?.evaluate((el) => el.textContent); assert(counterValue === "3");
const fontWeight = await counter?.evaluate((el) => getComputedStyle(el).fontWeight ); assertEquals(fontWeight, "700");
const buttonPlus = await page.$("body > div > div > button:nth-child(3)"); await buttonPlus?.click();
await delay(100);
counterValue = await counter?.evaluate((el) => el.textContent); assert(counterValue === "4"); await page.close(); await browser.close();
await lines.cancel(); serverProcess.kill("SIGTERM"); serverProcess.close(); }); }, sanitizeOps: false, sanitizeResources: false,});
Deno.test("fresh-init error(help)", async function (t) { const includeText = "fresh-init";
await t.step( "execute invalid init command (deno run -A init.ts)", async () => { const cliProcess = Deno.run({ cmd: ["deno", "run", "-A", "init.ts"], stdin: "null", stderr: "piped", }); const { code } = await cliProcess.status(); cliProcess.close(); assertEquals(code, 1);
const rawError = await cliProcess.stderrOutput(); const errorString = new TextDecoder().decode(rawError);
assertStringIncludes(errorString, includeText); }, );
await t.step( "execute invalid init command (deno run -A init.ts -f)", async () => { const cliProcess = Deno.run({ cmd: ["deno", "run", "-A", "init.ts", "-f"], stdin: "null", stderr: "piped", }); const { code } = await cliProcess.status(); cliProcess.close(); assertEquals(code, 1);
const rawError = await cliProcess.stderrOutput(); const errorString = new TextDecoder().decode(rawError);
assertStringIncludes(errorString, includeText); }, );
await t.step( "execute invalid init command (deno run -A init.ts --foo)", async () => { const cliProcess = Deno.run({ cmd: ["deno", "run", "-A", "init.ts", "--foo"], stdin: "null", stderr: "piped", }); const { code } = await cliProcess.status(); cliProcess.close(); assertEquals(code, 1);
const rawError = await cliProcess.stderrOutput(); const errorString = new TextDecoder().decode(rawError);
assertStringIncludes(errorString, includeText); }, );});
Version Info