/** * @jsxImportSource @solidjs/web * @vitest-environment jsdom */ // #4875: the server writes the tree as the JSX nests it, and when that // nesting is invalid HTML across a component boundary the browser's parser // rebuilds it: a
closes an open

, an closes an open , a // directly in a gets an implied , a inside a is // dropped. Hydration keeps the parser's tree; dev names the cause // (INVALID_HTML_NESTING, or the key-miss hint for a dropped ). The // markup below is renderToString's output for each app. import { afterEach, beforeEach, expect, test, vi } from "vitest"; import { hydrate } from "@solidjs/web"; import { createSignal, flush, For } from "solid-js"; const container = document.createElement("div"); let dispose: (() => void) | undefined; let warn: { mock: { calls: unknown[][] } }; beforeEach(() => { (globalThis as any)._$HY = { events: [], completed: new WeakSet(), r: {}, fe() {} }; document.body.appendChild(container); warn = vi.spyOn(console, "warn").mockImplementation(() => {}); }); afterEach(() => { dispose?.(); dispose = undefined; container.remove(); container.innerHTML = ""; vi.restoreAllMocks(); }); const nesting = () => warn.mock.calls.map(c => String(c[0])).filter(m => m.includes("[INVALID_HTML_NESTING]")); const Row = (props: { v: string }) => ( ); test("a
a component renders inside a

warns, naming where the parser put it", () => { const P = (props: { children: any }) =>

{props.children}

; const Badge = () =>
x
; container.innerHTML = '

x

'; expect(container.querySelector("p")!.children.length).toBe(0); dispose = hydrate( () => (

), container ); flush(); const msgs = nesting(); expect(msgs).toHaveLength(1); expect(msgs[0]).toContain("rendered
inside

"); expect(msgs[0]).toContain("it now sits in

"); }); test("an inside an warns", () => { const A = (props: { children: any }) => {props.children}; const Inner = () => in; container.innerHTML = '
in
'; dispose = hydrate( () => (
), container ); flush(); expect(nesting()).toHaveLength(1); expect(nesting()[0]).toContain("rendered inside "); }); test("rows straight into a
{props.v}
warn once, naming the implied ", () => { const [rows] = createSignal(["a", "b"]); container.innerHTML = "
a
b
"; expect(container.querySelector("table")!.firstElementChild!.localName).toBe("tbody"); dispose = hydrate( () => (

{v => }
), container ); flush(); const msgs = nesting(); expect(msgs).toHaveLength(1); expect(msgs[0]).toContain("wrapped it in a it added"); }); test("a inside a names the dropped form in the key miss", () => { const F = (props: { children: any }) => {props.children}; const G = () => (
); container.innerHTML = '
'; dispose = hydrate( () => (
), container ); flush(); const miss = warn.mock.calls.map(c => String(c[0])).filter(m => m.includes("Hydration key miss")); expect(miss).toHaveLength(1); expect(miss[0]).toContain("forms cannot nest"); }); test("valid nesting and an explicit hydrate, update and warn nothing", () => { const [rows, setRows] = createSignal(["a", "b"]); const Wrap = (props: { children: any }) =>
{props.children}
; container.innerHTML = '

t

a
b
'; dispose = hydrate( () => (

t

{v => }
), container ); flush(); setRows(r => [...r.slice(1), "z"]); flush(); expect(container.querySelector("tbody")!.textContent).toBe("bz"); expect(nesting()).toEqual([]); });