clone

a deno framework for local-first web apps that open instantly, work offline and sync when users choose

feat/prf-provision-custody
lofi / package / testing / readiness_test.ts
readiness_test.ts 1.6K
1import type { Page } from "npm:playwright@1.61.1";2import { ReadinessError, waitForReady } from "./readiness.ts";34function assert(condition: unknown, message: string): asserts condition {5 if (!condition) throw new Error(message);6}78Deno.test("waitForReady delegates polling and timeout to Playwright", async () => {9 let call: unknown[] | undefined;10 const page = {11 waitForFunction(...args: unknown[]) {12 call = args;13 return Promise.resolve({});14 },15 } as unknown as Page;16 const predicate = (expected: string) => document.body.dataset.ready === expected;1718 await waitForReady(page, predicate, "yes", {19 description: "reference app boot",20 timeoutMs: 321,21 polling: 17,22 });2324 assert(call?.[0] === predicate, "predicate was replaced");25 assert(call?.[1] === "yes", "predicate argument was replaced");26 assert(27 JSON.stringify(call?.[2]) === JSON.stringify({ polling: 17, timeout: 321 }),28 `unexpected Playwright options: ${JSON.stringify(call?.[2])}`,29 );30});3132Deno.test("waitForReady adds the app condition to timeout failures", async () => {33 const cause = new Error("playwright timeout");34 const page = {35 waitForFunction() {36 return Promise.reject(cause);37 },38 } as unknown as Page;3940 try {41 await waitForReady(page, () => false, undefined, { description: "sync indicator" });42 throw new Error("expected readiness to fail");43 } catch (error) {44 assert(error instanceof ReadinessError, `unexpected error: ${error}`);45 assert(error.message.includes("sync indicator"), "condition was missing from the error");46 assert(error.cause === cause, "Playwright failure was not retained as the cause");47 }48});