import { hydrate } from "solid-js/web"; import { createServer, type ViteDevServer } from "vite"; import solidPlugin from "vite-plugin-solid"; import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; import { SSR_HYDRATION_CONTROL_CASES, SSR_HYDRATION_MOTION_CASES, SsrHydrationFixture, renderIdForHydrationCase, type SsrHydrationCaseName, } from "./ssr-hydration-cases.client.tsx"; let server: ViteDevServer; beforeAll(async () => { server = await createServer({ appType: "custom", configFile: false, logLevel: "error", plugins: [ solidPlugin({ ssr: true, hot: false, solid: { generate: "ssr", hydratable: true, }, }), ], server: { middlewareMode: true }, }); }); afterAll(async () => { await server?.close(); }); afterEach(() => { document.body.textContent = ""; delete (window as typeof window & { _$HY?: unknown })._$HY; vi.restoreAllMocks(); }); type ServerFixtureModule = typeof import("./ssr-hydration-cases.server.tsx"); interface ServerHydrationMarkup { html: string; hydrationScript: string; } function flushHydrationEffects(): Promise { return Promise.resolve().then(() => Promise.resolve()); } async function renderServerHtml(name: SsrHydrationCaseName): Promise { return (await renderServerHydrationMarkup(name)).html; } async function renderServerHydrationMarkup(name: SsrHydrationCaseName): Promise { const mod = await server.ssrLoadModule("/src/test/ssr-hydration-cases.server.tsx") as ServerFixtureModule; return mod.renderSsrHydrationFixtureMarkup(name); } function captureConsoleDiagnostics() { const messages: string[] = []; const collect = (level: "error" | "warn") => (...args: unknown[]) => { messages.push(`${level}: ${args.map(String).join(" ")}`); }; vi.spyOn(console, "error").mockImplementation(collect("error")); vi.spyOn(console, "warn").mockImplementation(collect("warn")); return messages; } function hydrationDiagnostics(messages: readonly string[]): string[] { return messages.filter((message) => /hydration|mismatch|hydratable/i.test(message)); } function createHydrationRoot(serverHtml: string): HTMLElement { const root = document.createElement("main"); root.innerHTML = serverHtml; document.body.append(root); return root; } function executeHydrationScript(scriptHtml: string): void { const template = document.createElement("template"); template.innerHTML = scriptHtml.trim(); const code = template.content.querySelector("script")?.textContent; if (!code) { throw new Error("Expected Solid hydration script to contain executable code."); } window.eval(code); } function createHydratableRoot(markup: ServerHydrationMarkup): HTMLElement { executeHydrationScript(markup.hydrationScript); return createHydrationRoot(markup.html); } function expectTransformToContainTranslateX(element: HTMLElement | SVGElement, value: number): void { expect(element.getAttribute("style") ?? "").toContain(`translateX(${value}px)`); } async function expectCaseHydratesWithoutDiagnostics(name: SsrHydrationCaseName): Promise { const markup = await renderServerHydrationMarkup(name); expect( markup.html, `Expected server markup for ${name} to include Solid hydration markers before hydrate() runs.`, ).toContain("data-hk="); const root = createHydratableRoot(markup); const element = root.querySelector(`[data-testid='${name}']`) as HTMLElement | SVGElement | null; expect(element).toBeTruthy(); const diagnostics = captureConsoleDiagnostics(); const dispose = hydrate( () => , root, { renderId: renderIdForHydrationCase(name) }, ); await flushHydrationEffects(); dispose(); expect(hydrationDiagnostics(diagnostics)).toEqual([]); } describe("SSR + hydration", () => { it.each(SSR_HYDRATION_CONTROL_CASES)("hydrates control case %s without mismatch diagnostics", async (name) => { await expectCaseHydratesWithoutDiagnostics(name); }); it.each(SSR_HYDRATION_MOTION_CASES)("hydrates motion case %s without mismatch diagnostics", async (name) => { await expectCaseHydratesWithoutDiagnostics(name); }); it("serializes explicit initial styles before hydration", async () => { const root = createHydrationRoot(await renderServerHtml("explicit-initial")); const element = root.querySelector("[data-testid='explicit-initial']"); expect(element?.style.opacity).toBe("0"); expectTransformToContainTranslateX(element!, 4); }); it("serializes animate styles when initial is false", async () => { const root = createHydrationRoot(await renderServerHtml("initial-false")); const element = root.querySelector("[data-testid='initial-false']"); expect(element?.style.opacity).toBe("1"); expectTransformToContainTranslateX(element!, 12); }); it("serializes animate styles when initial is omitted", async () => { const root = createHydrationRoot(await renderServerHtml("omitted-initial")); const element = root.querySelector("[data-testid='omitted-initial']"); expect(element?.style.opacity).toBe("1"); expectTransformToContainTranslateX(element!, 12); }); it("serializes SVG styles without breaking hydration", async () => { const root = createHydrationRoot(await renderServerHtml("svg-style")); const element = root.querySelector("[data-testid='svg-style']"); expect(element?.tagName.toLowerCase()).toBe("svg"); expect(element?.style.opacity).toBe("0.5"); expect(element?.style.getPropertyValue("--svg-alpha")).toBe("0.4"); }); it("serializes CSS variables and transform styles together", async () => { const root = createHydrationRoot(await renderServerHtml("css-vars-transform")); const element = root.querySelector("[data-testid='css-vars-transform']"); expect(element?.style.opacity).toBe("1"); expect(element?.style.getPropertyValue("--brand-alpha")).toBe("0.75"); expectTransformToContainTranslateX(element!, 12); }); });