diff --git a/tests/cli_test.ts b/tests/cli_test.ts index 8b182a4c..815b7ee5 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -8,8 +8,8 @@ import { delay, puppeteer, retry, - TextLineStream, } from "./deps.ts"; +import { startFreshServer } from "./test_utils.ts"; type FileTree = { type: "file"; @@ -112,29 +112,10 @@ Deno.test({ }); await t.step("start up the server and access the root page", async () => { - const serverProcess = new Deno.Command(Deno.execPath(), { + const { serverProcess, lines } = await startFreshServer({ args: ["run", "-A", "--check", "main.ts"], - stdin: "null", - stdout: "piped", - stderr: "inherit", cwd: tmpDirName, - }).spawn(); - - const lines = serverProcess.stdout - .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); @@ -260,29 +241,10 @@ Deno.test({ }); await t.step("start up the server and access the root page", async () => { - const serverProcess = new Deno.Command(Deno.execPath(), { + const { serverProcess, lines } = await startFreshServer({ args: ["run", "-A", "--check", "main.ts"], - stdin: "null", - stdout: "piped", - stderr: "inherit", cwd: tmpDirName, - }).spawn(); - - const lines = serverProcess.stdout - .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); @@ -456,28 +418,10 @@ Deno.test({ }); await t.step("start up the server", async () => { - const serverProcess = new Deno.Command(Deno.execPath(), { + const { serverProcess, lines } = await startFreshServer({ args: ["run", "-A", "--check", "subdirectory/subsubdirectory/dev.ts"], - stdin: "null", - stdout: "piped", - stderr: "inherit", cwd: tmpDirName, - }).spawn(); - - const lines = serverProcess.stdout - .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; - } - } - - assert(started, "Server didn't start up"); + }); await delay(100); diff --git a/tests/main_test.ts b/tests/main_test.ts index b9ddf6fe..48f35d42 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -5,11 +5,11 @@ import { assertStringIncludes, delay, puppeteer, - TextLineStream, } from "./deps.ts"; import manifest from "./fixture/fresh.gen.ts"; import options from "./fixture/options.ts"; import { BUILD_ID } from "../src/server/build_id.ts"; +import { startFreshServer } from "./test_utils.ts"; const ctx = await ServerContext.fromManifest(manifest, options); const handler = ctx.handler(); @@ -606,7 +606,7 @@ Deno.test("experimental Deno.serve", { ignore: Deno.build.os === "windows", // TODO: Deno.serve hang on Windows? }, async (t) => { // Preparation - const serverProcess = new Deno.Command(Deno.execPath(), { + const { serverProcess, lines } = await startFreshServer({ args: [ "run", "-A", @@ -614,25 +614,7 @@ Deno.test("experimental Deno.serve", { "./tests/fixture/main.ts", "--experimental-deno-serve", ], - stdout: "piped", - stderr: "inherit", - }).spawn(); - - const decoder = new TextDecoderStream(); - const lines = serverProcess.stdout - .pipeThrough(decoder) - .pipeThrough(new TextLineStream()); - - let started = false; - for await (const line of lines) { - if (line.includes("Listening on http://")) { - started = true; - break; - } - } - if (!started) { - throw new Error("Server didn't start up"); - } + }); await delay(100); @@ -676,27 +658,9 @@ Deno.test("jsx pragma works", { sanitizeResources: false, }, async (t) => { // Preparation - const serverProcess = new Deno.Command(Deno.execPath(), { + const { serverProcess, lines } = await startFreshServer({ args: ["run", "-A", "./tests/fixture_jsx_pragma/main.ts"], - stdout: "piped", - stderr: "inherit", - }).spawn(); - - const decoder = new TextDecoderStream(); - const lines = serverProcess.stdout - .pipeThrough(decoder) - .pipeThrough(new TextLineStream()); - - let started = false; - for await (const line of lines) { - if (line.includes("Listening on http://")) { - started = true; - break; - } - } - if (!started) { - throw new Error("Server didn't start up"); - } + }); await delay(100); @@ -730,27 +694,9 @@ Deno.test("preloading javascript files", { sanitizeResources: false, }, async () => { // Preparation - const serverProcess = new Deno.Command(Deno.execPath(), { + const { serverProcess, lines } = await startFreshServer({ args: ["run", "-A", "./tests/fixture/main.ts"], - stdout: "piped", - stderr: "inherit", - }).spawn(); - - const decoder = new TextDecoderStream(); - const lines = serverProcess.stdout - .pipeThrough(decoder) - .pipeThrough(new TextLineStream()); - - let started = false; - for await (const line of lines) { - if (line.includes("Listening on http://")) { - started = true; - break; - } - } - if (!started) { - throw new Error("Server didn't start up"); - } + }); const browser = await puppeteer.launch({ args: ["--no-sandbox"] }); const page = await browser.newPage(); @@ -798,32 +744,10 @@ Deno.test("PORT environment variable", { }, async () => { const PORT = "8765"; // Preparation - const serverProcess = new Deno.Command(Deno.execPath(), { - args: [ - "run", - "-A", - "./tests/fixture/main.ts", - ], - stdout: "piped", - stderr: "inherit", + const { serverProcess, lines } = await startFreshServer({ + args: ["run", "-A", "./tests/fixture/main.ts"], env: { PORT }, - }).spawn(); - - const decoder = new TextDecoderStream(); - const lines = serverProcess.stdout - .pipeThrough(decoder) - .pipeThrough(new TextLineStream()); - - let started = false; - for await (const line of lines) { - if (line.includes("Listening on http://") && line.includes(PORT)) { - started = true; - break; - } - } - if (!started) { - throw new Error("Server didn't start up"); - } + }); await delay(100); diff --git a/tests/test_utils.ts b/tests/test_utils.ts new file mode 100644 index 00000000..e5c68812 --- /dev/null +++ b/tests/test_utils.ts @@ -0,0 +1,28 @@ +import { TextLineStream } from "./deps.ts"; + +export async function startFreshServer(options: Deno.CommandOptions) { + const serverProcess = new Deno.Command(Deno.execPath(), { + ...options, + stdin: "null", + stdout: "piped", + stderr: "inherit", + }).spawn(); + + const decoder = new TextDecoderStream(); + const lines: ReadableStream = serverProcess.stdout + .pipeThrough(decoder) + .pipeThrough(new TextLineStream()); + + let started = false; + for await (const line of lines) { + if (line.includes("Listening on http://")) { + started = true; + break; + } + } + if (!started) { + throw new Error("Server didn't start up"); + } + + return { serverProcess, lines }; +}