From 894f56b9c93415dc206aeecf9f3c2114123620f1 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 9 Jun 2023 11:27:56 +0200 Subject: [PATCH] chore(tests): use to spin up server (#1261) --- tests/islands_test.ts | 30 +++--------------------------- tests/islands_wasm_test.ts | 24 ++++-------------------- tests/plugin_test.ts | 24 +++--------------------- tests/twind_test.ts | 32 +++++--------------------------- www/main_test.ts | 25 +++---------------------- 5 files changed, 18 insertions(+), 117 deletions(-) diff --git a/tests/islands_test.ts b/tests/islands_test.ts index 27dc0855..5aa85d51 100644 --- a/tests/islands_test.ts +++ b/tests/islands_test.ts @@ -5,8 +5,8 @@ import { delay, Page, puppeteer, - TextLineStream, } from "./deps.ts"; +import { startFreshServer } from "./test_utils.ts"; Deno.test({ name: "island tests", @@ -64,24 +64,11 @@ function withPage(fn: (page: Page) => Promise) { } async function withPageName(name: string, fn: (page: Page) => Promise) { - const serverProcess = new Deno.Command(Deno.execPath(), { + const { lines, serverProcess } = await startFreshServer({ args: ["run", "-A", name], - stdout: "piped", - stderr: "inherit", - }).spawn(); - - const textDecoderStream = new TextDecoderStream(); - const textLineStream = new TextLineStream(); - - const lines = serverProcess.stdout - .pipeThrough(textDecoderStream) - .pipeThrough(textLineStream); + }); try { - if (!await didServerStart(lines)) { - throw new Error("Server didn't start up"); - } - await delay(100); const browser = await puppeteer.launch({ args: ["--no-sandbox"] }); @@ -101,17 +88,6 @@ async function withPageName(name: string, fn: (page: Page) => Promise) { } } -async function didServerStart( - stdoutLines: ReadableStream, -): Promise { - for await (const line of stdoutLines) { - if (line.includes("Listening on http://")) { - return true; - } - } - return false; -} - Deno.test({ name: "island tests with ", diff --git a/tests/islands_wasm_test.ts b/tests/islands_wasm_test.ts index 1cbd5185..0b4adc81 100644 --- a/tests/islands_wasm_test.ts +++ b/tests/islands_wasm_test.ts @@ -1,30 +1,14 @@ -import { assert, delay, puppeteer, TextLineStream } from "./deps.ts"; +import { assert, delay, puppeteer } from "./deps.ts"; +import { startFreshServer } from "./test_utils.ts"; Deno.test({ name: "wasm island tests", ignore: Deno.build.os === "windows", async fn(t) { // Preparation - const serverProcess = new Deno.Command(Deno.execPath(), { + const { lines, serverProcess } = await startFreshServer({ args: ["run", "-A", "./tests/fixture/main_wasm.ts"], - stdout: "piped", - }).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); diff --git a/tests/plugin_test.ts b/tests/plugin_test.ts index 86999f27..1ec9bbac 100644 --- a/tests/plugin_test.ts +++ b/tests/plugin_test.ts @@ -5,10 +5,10 @@ import { assertStringIncludes, delay, puppeteer, - TextLineStream, } from "./deps.ts"; import manifest from "./fixture_plugin/fresh.gen.ts"; import options from "./fixture_plugin/options.ts"; +import { startFreshServer } from "./test_utils.ts"; const ctx = await ServerContext.fromManifest(manifest, options); const handler = ctx.handler(); @@ -62,27 +62,9 @@ Deno.test({ name: "/with-island hydration", async fn(t) { // Preparation - const serverProcess = new Deno.Command(Deno.execPath(), { + const { lines, serverProcess } = await startFreshServer({ args: ["run", "-A", "./tests/fixture_plugin/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); diff --git a/tests/twind_test.ts b/tests/twind_test.ts index 62ee355a..66077d9f 100644 --- a/tests/twind_test.ts +++ b/tests/twind_test.ts @@ -1,6 +1,7 @@ -import { assert, delay, puppeteer, TextLineStream } from "./deps.ts"; +import { assert, delay, puppeteer } from "./deps.ts"; import { cmpStringArray } from "./fixture_twind_hydrate/utils/utils.ts"; +import { startFreshServer } from "./test_utils.ts"; /** * Start the server with the main file. @@ -8,34 +9,10 @@ import { cmpStringArray } from "./fixture_twind_hydrate/utils/utils.ts"; * Returns a page instance and a method to terminate the server. */ async function setUpServer(path: string) { - const serverProcessCmd = new Deno.Command("deno", { - args: [ - "run", - "-A", - path, - ], - stdout: "piped", - stderr: "inherit", + const { lines, serverProcess } = await startFreshServer({ + args: ["run", "-A", path], }); - const serverProcess = serverProcessCmd.spawn(); - - const lines = serverProcess.stdout - .pipeThrough(new TextDecoderStream()) - .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); const browser = await puppeteer.launch({ args: ["--no-sandbox"] }); @@ -45,6 +22,7 @@ async function setUpServer(path: string) { * terminate server */ const terminate = async () => { + await lines.cancel(); await browser.close(); serverProcess.kill("SIGKILL"); diff --git a/www/main_test.ts b/www/main_test.ts index d2803d57..49b0eec2 100644 --- a/www/main_test.ts +++ b/www/main_test.ts @@ -1,32 +1,13 @@ import { assertEquals } from "$std/testing/asserts.ts"; -import { TextLineStream } from "$std/streams/text_line_stream.ts"; import { delay } from "$std/async/delay.ts"; +import { startFreshServer } from "../tests/test_utils.ts"; Deno.test("CORS should not set on GET /fresh-badge.svg", { sanitizeResources: false, }, async () => { - const serverProcess = new Deno.Command(Deno.execPath(), { + const { serverProcess, lines } = await startFreshServer({ args: ["run", "-A", "./main.ts"], - stdin: "null", - 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 res = await fetch("http://localhost:8000/fresh-badge.svg"); await res.body?.cancel();