From 813ee225746d2384cf54a4a5b8d404fb85cd360c Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Tue, 1 Aug 2023 17:08:57 +0200 Subject: [PATCH] chore: speed up layout tests (#1565) --- tests/deps.ts | 7 ++- tests/layouts_test.ts | 54 ++++++++++----------- tests/test_utils.ts | 108 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 140 insertions(+), 29 deletions(-) diff --git a/tests/deps.ts b/tests/deps.ts index 652c44de..f91a8fcd 100644 --- a/tests/deps.ts +++ b/tests/deps.ts @@ -19,4 +19,9 @@ export { default as puppeteer, Page, } from "https://deno.land/x/puppeteer@16.2.0/mod.ts"; -export { DOMParser } from "https://esm.sh/linkedom@0.15.1"; +export { + Document, + DOMParser, + HTMLElement, + HTMLMetaElement, +} from "https://esm.sh/linkedom@0.15.1"; diff --git a/tests/layouts_test.ts b/tests/layouts_test.ts index c2f66dfc..157f632a 100644 --- a/tests/layouts_test.ts +++ b/tests/layouts_test.ts @@ -1,63 +1,63 @@ -import { withPageName } from "./test_utils.ts"; +import { assertSelector, fetchHtml, withFresh } from "./test_utils.ts"; Deno.test("apply root _layout and _app", async () => { - await withPageName( + await withFresh( "./tests/fixture_layouts/main.ts", - async (page, address) => { - await page.goto(address); - await page.waitForSelector(".app .root-layout .home-page"); + async (address) => { + const doc = await fetchHtml(address); + assertSelector(doc, ".app .root-layout .home-page"); - await page.goto(`${address}/other`); - await page.waitForSelector(".app .root-layout .other-page"); + const doc2 = await fetchHtml(`${address}/other`); + assertSelector(doc2, ".app .root-layout .other-page"); }, ); }); Deno.test("apply sub layouts", async () => { - await withPageName( + await withFresh( "./tests/fixture_layouts/main.ts", - async (page, address) => { - await page.goto(`${address}/foo`); - await page.waitForSelector(".app .root-layout .foo-layout .foo-page"); + async (address) => { + const doc = await fetchHtml(`${address}/foo`); + assertSelector(doc, ".app .root-layout .foo-layout .foo-page"); - await page.goto(`${address}/foo/bar`); - await page.waitForSelector(".app .root-layout .foo-layout .bar-page"); + const doc2 = await fetchHtml(`${address}/foo/bar`); + assertSelector(doc2, ".app .root-layout .foo-layout .bar-page"); }, ); }); Deno.test("skip layouts if not present", async () => { - await withPageName( + await withFresh( "./tests/fixture_layouts/main.ts", - async (page, address) => { - await page.goto(`${address}/skip/sub`); - await page.waitForSelector(".app .root-layout .sub-layout .sub-page"); + async (address) => { + const doc = await fetchHtml(`${address}/skip/sub`); + assertSelector(doc, ".app .root-layout .sub-layout .sub-page"); }, ); }); Deno.test("check file types", async (t) => { - await withPageName( + await withFresh( "./tests/fixture_layouts/main.ts", - async (page, address) => { + async (address) => { await t.step(".js", async () => { - await page.goto(`${address}/files/js`); - await page.waitForSelector(".app .root-layout .js-layout .js-page"); + const doc = await fetchHtml(`${address}/files/js`); + assertSelector(doc, ".app .root-layout .js-layout .js-page"); }); await t.step(".jsx", async () => { - await page.goto(`${address}/files/jsx`); - await page.waitForSelector(".app .root-layout .jsx-layout .jsx-page"); + const doc = await fetchHtml(`${address}/files/jsx`); + assertSelector(doc, ".app .root-layout .jsx-layout .jsx-page"); }); await t.step(".ts", async () => { - await page.goto(`${address}/files/ts`); - await page.waitForSelector(".app .root-layout .ts-layout .ts-page"); + const doc = await fetchHtml(`${address}/files/ts`); + assertSelector(doc, ".app .root-layout .ts-layout .ts-page"); }); await t.step(".tsx", async () => { - await page.goto(`${address}/files/tsx`); - await page.waitForSelector(".app .root-layout .tsx-layout .tsx-page"); + const doc = await fetchHtml(`${address}/files/tsx`); + assertSelector(doc, ".app .root-layout .tsx-layout .tsx-page"); }); }, ); diff --git a/tests/test_utils.ts b/tests/test_utils.ts index 632dfe40..f8a80ca2 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -1,4 +1,13 @@ -import { delay, DOMParser, Page, puppeteer, TextLineStream } from "./deps.ts"; +import { colors } from "$fresh/src/server/deps.ts"; +import { + delay, + DOMParser, + HTMLElement, + HTMLMetaElement, + Page, + puppeteer, + TextLineStream, +} from "./deps.ts"; export function parseHtml(input: string) { return new DOMParser().parseFromString(input, "text/html"); @@ -14,6 +23,103 @@ export async function startFreshServer(options: Deno.CommandOptions) { return { serverProcess, lines, address }; } +export async function fetchHtml(url: string) { + const res = await fetch(url); + const html = await res.text(); + // deno-lint-ignore no-explicit-any + return new DOMParser().parseFromString(html, "text/html") as any as Document; +} + +export function assertSelector(doc: Document, selector: string) { + if (doc.querySelector(selector) === null) { + const html = prettyDom(doc); + throw new Error( + `Selector "${selector}" not found in document.\n\n${html}`, + ); + } +} + +export const VOID_ELEMENTS = + /^(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/; +function prettyDom(doc: Document) { + let out = colors.dim(`\n`); + console.log(out); + + const node = doc.documentElement; + out += _printDomNode(node, 0); + + return out; +} + +function _printDomNode( + node: HTMLElement | Text | Node, + indent: number, +) { + const space = " ".repeat(indent); + + if (node.nodeType === 3) { + return space + colors.dim(node.textContent ?? "") + "\n"; + } + + let out = space; + if (node instanceof HTMLElement || node instanceof HTMLMetaElement) { + out += colors.dim(colors.cyan("<")); + out += colors.cyan(node.localName); + + for (let i = 0; i < node.attributes.length; i++) { + const attr = node.attributes.item(i); + if (attr === null) continue; + out += " " + colors.yellow(attr.name); + out += colors.dim("="); + out += colors.green(`"${attr.value}"`); + } + + if (VOID_ELEMENTS.test(node.localName)) { + out += colors.dim(colors.cyan(">")) + "\n"; + return out; + } + + out += colors.dim(colors.cyan(">")); + if (node.childNodes.length) { + out += "\n"; + + for (let i = 0; i < node.childNodes.length; i++) { + const child = node.childNodes[i]; + out += _printDomNode(child, indent + 1); + } + + out += space; + } + + out += colors.dim(colors.cyan("")); + out += "\n"; + } + + return out; +} + +export async function withFresh( + name: string, + fn: (address: string) => Promise, +) { + const { lines, serverProcess, address } = await startFreshServer({ + args: ["run", "-A", name], + }); + + try { + await fn(address); + } finally { + await lines.cancel(); + + serverProcess.kill("SIGTERM"); + + // Wait until the process exits + await serverProcess.status; + } +} + export async function withPageName( name: string, fn: (page: Page, address: string) => Promise,