refactor: add integrationTest() helper to reduce test boilerplate (#3711)
## Summary - Adds an `integrationTest()` wrapper in `packages/fresh/src/test_utils.ts` that calls `Deno.test` with `sanitizeOps: false` and `sanitizeResources: false` - Converts ~88 integration tests across 5 files to use the new helper, removing repetitive sanitizer boilerplate - Net reduction of ~344 lines (-1678/+1334) --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
196509274f
commit
a872a70e5f
File diff suppressed because it is too large
Load Diff
@@ -182,6 +182,26 @@ export class MockBuildCache<State> implements BuildCache<State> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around `Deno.test` for integration tests that disables sanitizers.
|
||||
* Accepts either a name string or an options object (without `fn`), plus the
|
||||
* test function.
|
||||
*/
|
||||
export function integrationTest(
|
||||
nameOrOptions: string | Omit<Deno.TestDefinition, "fn">,
|
||||
fn: () => void | Promise<void>,
|
||||
): void {
|
||||
const options = typeof nameOrOptions === "string"
|
||||
? { name: nameOrOptions }
|
||||
: nameOrOptions;
|
||||
Deno.test({
|
||||
...options,
|
||||
fn,
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
}
|
||||
|
||||
export async function writeFiles(dir: string, files: Record<string, string>) {
|
||||
const entries = Object.entries(files);
|
||||
await Promise.all(entries.map(async (entry) => {
|
||||
|
||||
+179
-219
@@ -8,7 +8,7 @@ import {
|
||||
withBrowserApp,
|
||||
} from "./test_utils.tsx";
|
||||
import { expect } from "@std/expect";
|
||||
import { FakeServer } from "../src/test_utils.ts";
|
||||
import { FakeServer, integrationTest } from "../src/test_utils.ts";
|
||||
import * as path from "@std/path";
|
||||
|
||||
const applyHeadCache = await buildProd({
|
||||
@@ -187,277 +187,237 @@ Deno.test("Head - ssr - updates link", async () => {
|
||||
expect(link.href).toEqual("https://example.com/ok");
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Head - client - set title",
|
||||
fn: async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
integrationTest("Head - client - set title", async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
|
||||
applyHeadCache(app);
|
||||
applyHeadCache(app);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/title`);
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/title`);
|
||||
|
||||
await page.locator(".ready").wait();
|
||||
await page.locator("button").click();
|
||||
await page.locator(".ready").wait();
|
||||
await page.locator("button").click();
|
||||
|
||||
await waitForText(page, "title", "Count: 1");
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
await waitForText(page, "title", "Count: 1");
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Head - client - match meta",
|
||||
fn: async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
integrationTest("Head - client - match meta", async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
|
||||
applyHeadCache(app);
|
||||
applyHeadCache(app);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/meta`);
|
||||
await page.locator(".ready").wait();
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/meta`);
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
await waitFor(async () => {
|
||||
const metas = await page.evaluate(() => {
|
||||
const metas = Array.from(
|
||||
document.querySelectorAll("meta[name]"),
|
||||
) as HTMLMetaElement[];
|
||||
await waitFor(async () => {
|
||||
const metas = await page.evaluate(() => {
|
||||
const metas = Array.from(
|
||||
document.querySelectorAll("meta[name]"),
|
||||
) as HTMLMetaElement[];
|
||||
|
||||
return metas.map((
|
||||
el: HTMLMetaElement,
|
||||
) => ({ name: el.name, content: el.content }));
|
||||
});
|
||||
|
||||
expect(metas).toEqual([
|
||||
{ name: "foo", content: "ok" },
|
||||
{ name: "bar", content: "not ok" },
|
||||
]);
|
||||
return true;
|
||||
return metas.map((
|
||||
el: HTMLMetaElement,
|
||||
) => ({ name: el.name, content: el.content }));
|
||||
});
|
||||
|
||||
expect(metas).toEqual([
|
||||
{ name: "foo", content: "ok" },
|
||||
{ name: "bar", content: "not ok" },
|
||||
]);
|
||||
return true;
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Head - client - match style by id",
|
||||
fn: async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
integrationTest("Head - client - match style by id", async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
|
||||
applyHeadCache(app);
|
||||
applyHeadCache(app);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/id`);
|
||||
await page.locator(".ready").wait();
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/id`);
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
await waitFor(async () => {
|
||||
const styles = await page.evaluate(() => {
|
||||
const els = Array.from(
|
||||
document.querySelectorAll("head style"),
|
||||
) as HTMLStyleElement[];
|
||||
await waitFor(async () => {
|
||||
const styles = await page.evaluate(() => {
|
||||
const els = Array.from(
|
||||
document.querySelectorAll("head style"),
|
||||
) as HTMLStyleElement[];
|
||||
|
||||
return els.map((
|
||||
el: HTMLStyleElement,
|
||||
) => ({ id: el.id, text: el.textContent }));
|
||||
});
|
||||
|
||||
expect(styles).toEqual([
|
||||
{ id: "", text: "not ok" },
|
||||
{ id: "style-id", text: "ok" },
|
||||
]);
|
||||
return true;
|
||||
return els.map((
|
||||
el: HTMLStyleElement,
|
||||
) => ({ id: el.id, text: el.textContent }));
|
||||
});
|
||||
|
||||
expect(styles).toEqual([
|
||||
{ id: "", text: "not ok" },
|
||||
{ id: "style-id", text: "ok" },
|
||||
]);
|
||||
return true;
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Head - client - match key",
|
||||
fn: async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
integrationTest("Head - client - match key", async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
|
||||
applyHeadCache(app);
|
||||
applyHeadCache(app);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/key`);
|
||||
await page.locator(".ready").wait();
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/key`);
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
await waitFor(async () => {
|
||||
const tpls = await page.evaluate(() => {
|
||||
const els = Array.from(
|
||||
document.querySelectorAll("head template"),
|
||||
) as HTMLTemplateElement[];
|
||||
await waitFor(async () => {
|
||||
const tpls = await page.evaluate(() => {
|
||||
const els = Array.from(
|
||||
document.querySelectorAll("head template"),
|
||||
) as HTMLTemplateElement[];
|
||||
|
||||
return els.map((
|
||||
el: HTMLTemplateElement,
|
||||
) => ({
|
||||
key: el.getAttribute("data-key"),
|
||||
text: el.content.textContent,
|
||||
}));
|
||||
});
|
||||
|
||||
expect(tpls).toEqual([
|
||||
{ key: "a", text: "ok" },
|
||||
{ key: "b", text: "not ok" },
|
||||
{ key: null, text: "not ok" },
|
||||
]);
|
||||
return true;
|
||||
return els.map((
|
||||
el: HTMLTemplateElement,
|
||||
) => ({
|
||||
key: el.getAttribute("data-key"),
|
||||
text: el.content.textContent,
|
||||
}));
|
||||
});
|
||||
|
||||
expect(tpls).toEqual([
|
||||
{ key: "a", text: "ok" },
|
||||
{ key: "b", text: "not ok" },
|
||||
{ key: null, text: "not ok" },
|
||||
]);
|
||||
return true;
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Head - client - dynamic meta update",
|
||||
fn: async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
integrationTest("Head - client - dynamic meta update", async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
|
||||
applyHeadCache(app);
|
||||
applyHeadCache(app);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/dynamic_meta`);
|
||||
await page.locator(".ready").wait();
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/dynamic_meta`);
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
// Verify initial meta value
|
||||
await waitFor(async () => {
|
||||
const content = await page.evaluate(() => {
|
||||
const el = document.querySelector(
|
||||
"meta[name='foo']",
|
||||
) as HTMLMetaElement;
|
||||
return el?.content;
|
||||
});
|
||||
expect(content).toEqual("value-0");
|
||||
return true;
|
||||
});
|
||||
|
||||
// Click to update and verify meta changes reactively
|
||||
await page.locator("button").click();
|
||||
|
||||
await waitFor(async () => {
|
||||
const content = await page.evaluate(() => {
|
||||
const el = document.querySelector(
|
||||
"meta[name='foo']",
|
||||
) as HTMLMetaElement;
|
||||
return el?.content;
|
||||
});
|
||||
expect(content).toEqual("value-1");
|
||||
return true;
|
||||
// Verify initial meta value
|
||||
await waitFor(async () => {
|
||||
const content = await page.evaluate(() => {
|
||||
const el = document.querySelector(
|
||||
"meta[name='foo']",
|
||||
) as HTMLMetaElement;
|
||||
return el?.content;
|
||||
});
|
||||
expect(content).toEqual("value-0");
|
||||
return true;
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
|
||||
// Click to update and verify meta changes reactively
|
||||
await page.locator("button").click();
|
||||
|
||||
await waitFor(async () => {
|
||||
const content = await page.evaluate(() => {
|
||||
const el = document.querySelector(
|
||||
"meta[name='foo']",
|
||||
) as HTMLMetaElement;
|
||||
return el?.content;
|
||||
});
|
||||
expect(content).toEqual("value-1");
|
||||
return true;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Head - client - link element",
|
||||
fn: async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
integrationTest("Head - client - link element", async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
|
||||
applyHeadCache(app);
|
||||
applyHeadCache(app);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/link`);
|
||||
await page.locator(".ready").wait();
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/link`);
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
await waitFor(async () => {
|
||||
const href = await page.evaluate(() => {
|
||||
const el = document.querySelector(
|
||||
"link[rel='canonical']",
|
||||
) as HTMLLinkElement;
|
||||
return el?.href;
|
||||
});
|
||||
expect(href).toEqual("https://example.com/ok");
|
||||
return true;
|
||||
await waitFor(async () => {
|
||||
const href = await page.evaluate(() => {
|
||||
const el = document.querySelector(
|
||||
"link[rel='canonical']",
|
||||
) as HTMLLinkElement;
|
||||
return el?.href;
|
||||
});
|
||||
expect(href).toEqual("https://example.com/ok");
|
||||
return true;
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Head - client - multiple islands with Head",
|
||||
fn: async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
integrationTest("Head - client - multiple islands with Head", async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
|
||||
applyHeadCache(app);
|
||||
applyHeadCache(app);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/multi`);
|
||||
await page.locator(".ready-a").wait();
|
||||
await page.locator(".ready-b").wait();
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/multi`);
|
||||
await page.locator(".ready-a").wait();
|
||||
await page.locator(".ready-b").wait();
|
||||
|
||||
await waitFor(async () => {
|
||||
const title = await page.evaluate(() => document.title);
|
||||
expect(title).toEqual("from island A");
|
||||
return true;
|
||||
});
|
||||
|
||||
await waitFor(async () => {
|
||||
const metas = await page.evaluate(() => {
|
||||
return {
|
||||
author: (document.querySelector(
|
||||
"meta[name='author']",
|
||||
) as HTMLMetaElement)?.content,
|
||||
description: (document.querySelector(
|
||||
"meta[name='description']",
|
||||
) as HTMLMetaElement)?.content,
|
||||
};
|
||||
});
|
||||
expect(metas.author).toEqual("island-a");
|
||||
expect(metas.description).toEqual("from-island-b");
|
||||
return true;
|
||||
});
|
||||
await waitFor(async () => {
|
||||
const title = await page.evaluate(() => document.title);
|
||||
expect(title).toEqual("from island A");
|
||||
return true;
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
|
||||
await waitFor(async () => {
|
||||
const metas = await page.evaluate(() => {
|
||||
return {
|
||||
author: (document.querySelector(
|
||||
"meta[name='author']",
|
||||
) as HTMLMetaElement)?.content,
|
||||
description: (document.querySelector(
|
||||
"meta[name='description']",
|
||||
) as HTMLMetaElement)?.content,
|
||||
};
|
||||
});
|
||||
expect(metas.author).toEqual("island-a");
|
||||
expect(metas.description).toEqual("from-island-b");
|
||||
return true;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Head - client - title updates multiple times",
|
||||
fn: async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
integrationTest("Head - client - title updates multiple times", async () => {
|
||||
const app = new App({})
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
|
||||
applyHeadCache(app);
|
||||
applyHeadCache(app);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/title`);
|
||||
await page.locator(".ready").wait();
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/title`);
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "title", "Count: 1");
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "title", "Count: 1");
|
||||
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "title", "Count: 2");
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "title", "Count: 2");
|
||||
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "title", "Count: 3");
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "title", "Count: 3");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user