chore: extract withPageName util (#1389)

This commit is contained in:
Marvin Hagemeister
2023-07-02 13:14:53 +02:00
committed by GitHub
parent 582f56b033
commit ced2215446
2 changed files with 30 additions and 31 deletions
+1 -30
View File
@@ -4,9 +4,8 @@ import {
assertStringIncludes,
delay,
Page,
puppeteer,
} from "./deps.ts";
import { startFreshServer } from "./test_utils.ts";
import { withPageName } from "./test_utils.ts";
Deno.test({
name: "island tests",
@@ -63,34 +62,6 @@ function withPage(fn: (page: Page, address: string) => Promise<void>) {
return withPageName("./tests/fixture/main.ts", fn);
}
async function withPageName(
name: string,
fn: (page: Page, address: string) => Promise<void>,
) {
const { lines, serverProcess, address } = await startFreshServer({
args: ["run", "-A", name],
});
try {
await delay(100);
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
try {
const page = await browser.newPage();
await fn(page, address);
} finally {
await browser.close();
}
} finally {
await lines.cancel();
serverProcess.kill("SIGTERM");
// Wait until the process exits
await serverProcess.status;
}
}
Deno.test({
name: "island tests with </script>",
+29 -1
View File
@@ -1,4 +1,4 @@
import { TextLineStream } from "./deps.ts";
import { delay, Page, puppeteer, TextLineStream } from "./deps.ts";
export async function startFreshServer(options: Deno.CommandOptions) {
const serverProcess = new Deno.Command(Deno.execPath(), {
@@ -29,3 +29,31 @@ export async function startFreshServer(options: Deno.CommandOptions) {
return { serverProcess, lines, address };
}
export async function withPageName(
name: string,
fn: (page: Page, address: string) => Promise<void>,
) {
const { lines, serverProcess, address } = await startFreshServer({
args: ["run", "-A", name],
});
try {
await delay(100);
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
try {
const page = await browser.newPage();
await fn(page, address);
} finally {
await browser.close();
}
} finally {
await lines.cancel();
serverProcess.kill("SIGTERM");
// Wait until the process exits
await serverProcess.status;
}
}