Skip to content

Commit 8690763

Browse files
committed
test(hydration): the #4348 spec stubs _$HY as the real bootstrap does
The spec stubbed `_$HY.r` as a plain object, so the `inner` shape's serialized promise, rejected by the stream before the memo under the content exists, raised an unhandled rejection the real bootstrap never raises: its `r` is a Proxy that catches every promise filed under it (#3769). A shared helper, test/harness/hydration-bootstrap.ts, installs that global; the spec uses it and runs `inner` streamed too. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent bc4db2e commit 8690763

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* The `_$HY` global as the real bootstrap installs it
3+
* (`generateHydrationScript()` / `<HydrationScript />` in src/server.ts), for
4+
* hydrate specs that replay server output without running the bootstrap.
5+
*
6+
* What a plain `{ r: {} }` stub misses: since #3769 `r` is a Proxy that
7+
* attaches a no-op catch to every promise filed under it, so a record the
8+
* client reads only later (a memo created when a boundary resumes, or a
9+
* client bundle that runs after the inline scripts) does not raise
10+
* `unhandledrejection` when the stream rejects it first. A consumer still
11+
* meets the rejection.
12+
*/
13+
export function bootstrapHydrationGlobal(): any {
14+
return ((globalThis as any)._$HY = {
15+
events: [],
16+
completed: new WeakSet(),
17+
r: new Proxy({} as Record<string, unknown>, {
18+
set: (r, k, v) => (v instanceof Promise && v.catch(() => {}), (r[k as string] = v), true)
19+
}),
20+
fe() {}
21+
});
22+
}

‎packages/web/test/hydration/errored-lazy-content-4348.spec.tsx‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
setLazyErroredKey,
3333
type LazyErroredShape
3434
} from "../harness/scenarios.jsx";
35+
import { bootstrapHydrationGlobal } from "../harness/hydration-bootstrap.js";
3536

3637
const artifact = (name: string) =>
3738
resolve(dirname(fileURLToPath(import.meta.url)), `../harness/__artifacts__/${name}.json`);
@@ -60,7 +61,7 @@ async function run(shape: LazyErroredShape, mode: "loaded" | "streamed") {
6061
const App = lazyErroredContent(shape);
6162
const container = document.createElement("div");
6263
document.body.appendChild(container);
63-
(globalThis as any)._$HY = { events: [], completed: new WeakSet(), r: {}, fe() {} };
64+
bootstrapHydrationGlobal();
6465
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
6566
const asksBefore = lazyErroredAskCount();
6667
let dispose: (() => void) | undefined;
@@ -73,16 +74,18 @@ async function run(shape: LazyErroredShape, mode: "loaded" | "streamed") {
7374
await settle();
7475
if (mode === "streamed") {
7576
expect(container.textContent).toBe("loadingtail");
76-
// The pieces: the fallback's swap with the error, the fragment's
77-
// settle, then (`loading`) the nested Loading's own fragment, which
78-
// the server still streams for the content it dropped.
79-
const pieces = rest.match(/[\s\S]*?<\/script>/g)!;
77+
// The pieces: the fallback's swap with the error (after, for `inner`,
78+
// the rejection of the memo created under the content), the
79+
// fragment's settle, then (`loading`) the nested Loading's own
80+
// fragment, which the server still streams for the content it dropped.
81+
const pieces: string[] = rest.match(/[\s\S]*?<\/script>/g)!;
82+
const swap = pieces.findIndex(p => p.includes("<template id="));
8083
for (let i = 0; i < pieces.length; i++) {
8184
applyChunk(container, pieces[i], false);
8285
await settle();
8386
// Once the boundary's fragment settled, hydration is over: nothing
8487
// under the content waits to claim — that nested fragment included.
85-
if (i > 0) expect(sharedConfig.isHydrationInProgress!()).toBe(false);
88+
if (i > swap) expect(sharedConfig.isHydrationInProgress!()).toBe(false);
8689
}
8790
serverP = container.querySelector("p");
8891
}
@@ -126,11 +129,6 @@ async function run(shape: LazyErroredShape, mode: "loaded" | "streamed") {
126129
describe("#4348: a hydrated Errored fallback over lazily created content", () => {
127130
for (const shape of lazyErroredShapes) {
128131
test(`${shape}: hydrated after the failure arrived`, () => run(shape, "loaded"));
129-
// (`inner` streamed: the memo created under the content asks for its
130-
// serialized answer only once the fragment lands, after the stream
131-
// rejected it — an unhandled rejection of its own, created in the
132-
// content's run as much as lazily, so only hydrated after.)
133-
if (shape !== "inner")
134-
test(`${shape}: hydrated before the failure arrived`, () => run(shape, "streamed"));
132+
test(`${shape}: hydrated before the failure arrived`, () => run(shape, "streamed"));
135133
}
136134
});

0 commit comments

Comments
 (0)