chore(test): extract shared start server code (#1250)

Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
Marvin Hagemeister
2023-06-05 18:52:22 +02:00
committed by GitHub
parent da5d507b77
commit cd760b90d4
3 changed files with 45 additions and 149 deletions
+7 -63
View File
@@ -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);
+10 -86
View File
@@ -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);
+28
View File
@@ -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<string> = 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 };
}