tests: use the same browser instance across tests and better use explicit resource management (#2901)
On my machine, reduces time taken to perform `deno task test` from ~1m20s to ~30s. Inspired by and supersedes #2110. --------- Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
co-authored by
Marvin Hagemeister
parent
17257dd1c2
commit
da8879dede
+14
-29
@@ -1,7 +1,7 @@
|
||||
import { App, getIslandRegistry, setBuildCache } from "../src/app.ts";
|
||||
import { launch, type Page } from "@astral/astral";
|
||||
import * as colors from "@std/fmt/colors";
|
||||
import { type Document, DOMParser, HTMLElement } from "linkedom";
|
||||
import { DOMParser, HTMLElement } from "linkedom";
|
||||
import { Builder } from "../src/dev/builder.ts";
|
||||
import { TextLineStream } from "@std/streams/text-line-stream";
|
||||
import * as path from "@std/path";
|
||||
@@ -28,6 +28,16 @@ import { NodeProcess } from "./fixtures_islands/NodeProcess.tsx";
|
||||
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
|
||||
import { OptOutPartialLink } from "./fixtures_islands/OptOutPartialLink.tsx";
|
||||
|
||||
const browser = await launch({
|
||||
args: [
|
||||
"--window-size=1280,720",
|
||||
...((Deno.env.get("CI") && Deno.build.os === "linux")
|
||||
? ["--no-sandbox"]
|
||||
: []),
|
||||
],
|
||||
headless: true,
|
||||
});
|
||||
|
||||
export function getIsland(pathname: string) {
|
||||
return path.join(
|
||||
import.meta.dirname!,
|
||||
@@ -79,45 +89,23 @@ export async function withBrowserApp(
|
||||
fn: (page: Page, address: string) => void | Promise<void>,
|
||||
) {
|
||||
const aborter = new AbortController();
|
||||
const server = Deno.serve({
|
||||
await using server = Deno.serve({
|
||||
hostname: "localhost",
|
||||
port: 0,
|
||||
signal: aborter.signal,
|
||||
onListen: () => {}, // Don't spam terminal with "Listening on..."
|
||||
}, app.handler());
|
||||
|
||||
const browser = await launch({
|
||||
args: [
|
||||
"--window-size=1280,720",
|
||||
...((Deno.env.get("CI") && Deno.build.os === "linux")
|
||||
? ["--no-sandbox"]
|
||||
: []),
|
||||
],
|
||||
headless: true,
|
||||
});
|
||||
|
||||
const page = await browser.newPage();
|
||||
try {
|
||||
await using page = await browser.newPage();
|
||||
await fn(page, `http://localhost:${server.addr.port}`);
|
||||
} finally {
|
||||
await page.close();
|
||||
await browser.close();
|
||||
aborter.abort();
|
||||
await server?.finished;
|
||||
}
|
||||
}
|
||||
|
||||
export async function withBrowser(fn: (page: Page) => void | Promise<void>) {
|
||||
const browser = await launch({
|
||||
args: [
|
||||
"--window-size=1280,7201",
|
||||
...((Deno.env.get("CI") && Deno.build.os === "linux")
|
||||
? ["--no-sandbox"]
|
||||
: []),
|
||||
],
|
||||
headless: true,
|
||||
});
|
||||
const page = await browser.newPage();
|
||||
await using page = await browser.newPage();
|
||||
try {
|
||||
await fn(page);
|
||||
} catch (err) {
|
||||
@@ -127,9 +115,6 @@ export async function withBrowser(fn: (page: Page) => void | Promise<void>) {
|
||||
// deno-lint-ignore no-console
|
||||
console.log(html);
|
||||
throw err;
|
||||
} finally {
|
||||
await page.close();
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user