|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * |
| 4 | + * solidjs/solid#4647: a serialized async value whose answer the serializer |
| 5 | + * cannot handle. The key already went out to the client as a pending |
| 6 | + * promise and nothing will ever settle it; the hook heard |
| 7 | + * `render/serialize`, yet the render completed and `onCompleteAll` reported |
| 8 | + * the document whole. A rejected value in the same spot fails the render; |
| 9 | + * so does this one. A value that fails as it is handed over never reaches |
| 10 | + * the client and still completes (#4227). |
| 11 | + */ |
| 12 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 13 | +import { renderToStream, type ServerErrorContext } from "@solidjs/web"; |
| 14 | +import { createMemo, Loading, onCleanup } from "solid-js"; |
| 15 | +import { settle, watchEscapes } from "./stream-settle-harness.js"; |
| 16 | + |
| 17 | +class Unserializable {} |
| 18 | + |
| 19 | +let heard: string[]; |
| 20 | +let ran: string[]; |
| 21 | +const onError = (_: unknown, context: ServerErrorContext) => |
| 22 | + void heard.push(`${context.kind}/${context.handling}`); |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + heard = []; |
| 26 | + ran = []; |
| 27 | +}); |
| 28 | +afterEach(() => { |
| 29 | + vi.restoreAllMocks(); |
| 30 | +}); |
| 31 | + |
| 32 | +function App() { |
| 33 | + onCleanup(() => ran.push("cleanup")); |
| 34 | + const value = createMemo(() => Promise.resolve(new Unserializable())); |
| 35 | + return <main>{String(value() instanceof Unserializable)}</main>; |
| 36 | +} |
| 37 | + |
| 38 | +/** A page whose boundary value settles once `gate` opens. */ |
| 39 | +function late(gate: Promise<void>) { |
| 40 | + return () => { |
| 41 | + onCleanup(() => ran.push("cleanup")); |
| 42 | + const value = createMemo(() => gate.then(() => new Unserializable())); |
| 43 | + return ( |
| 44 | + <main> |
| 45 | + <Loading fallback="pending"> |
| 46 | + <p>{String(value() instanceof Unserializable)}</p> |
| 47 | + </Loading> |
| 48 | + </main> |
| 49 | + ); |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +describe("renderToStream: an async value that fails to serialize (#4647)", () => { |
| 54 | + test("the awaited render fails like a rejection: no HTML, no onCompleteAll", async () => { |
| 55 | + let completed = false; |
| 56 | + let result: { state: string; html: string } | undefined; |
| 57 | + const escaped = await watchEscapes(async () => { |
| 58 | + result = await settle( |
| 59 | + renderToStream(App, { |
| 60 | + onError, |
| 61 | + onCompleteAll() { |
| 62 | + completed = true; |
| 63 | + } |
| 64 | + }) |
| 65 | + ); |
| 66 | + }); |
| 67 | + expect(escaped).toEqual([]); |
| 68 | + expect(result!.state).toBe("settled"); |
| 69 | + expect(result!.html).toBe(""); |
| 70 | + expect(completed).toBe(false); |
| 71 | + expect(heard).toEqual(["render/serialize"]); |
| 72 | + expect(ran).toEqual(["cleanup"]); |
| 73 | + }); |
| 74 | + |
| 75 | + test("after the shell, the piped stream ends without onCompleteAll", async () => { |
| 76 | + let release!: () => void; |
| 77 | + const gate = new Promise<void>(r => (release = r)); |
| 78 | + let completed = false; |
| 79 | + let result: { state: string; html: string } | undefined; |
| 80 | + const escaped = await watchEscapes(async () => { |
| 81 | + result = await settle( |
| 82 | + new Promise<string>(resolve => { |
| 83 | + const chunks: string[] = []; |
| 84 | + renderToStream(late(gate), { |
| 85 | + onError, |
| 86 | + onCompleteAll() { |
| 87 | + completed = true; |
| 88 | + } |
| 89 | + }).pipe({ |
| 90 | + write(chunk: string) { |
| 91 | + chunks.push(String(chunk)); |
| 92 | + release(); |
| 93 | + }, |
| 94 | + end() { |
| 95 | + resolve(chunks.join("")); |
| 96 | + } |
| 97 | + }); |
| 98 | + }) |
| 99 | + ); |
| 100 | + }); |
| 101 | + expect(escaped).toEqual([]); |
| 102 | + expect(result!.state).toBe("settled"); |
| 103 | + expect(result!.html).toContain("pending"); |
| 104 | + expect(completed).toBe(false); |
| 105 | + expect(heard).toEqual(["render/serialize"]); |
| 106 | + expect(ran).toEqual(["cleanup"]); |
| 107 | + }); |
| 108 | + |
| 109 | + test("a custom serializer's late failure is its own to settle: the render completes", async () => { |
| 110 | + // Reports every promise's answer as a failure, then settles the write. |
| 111 | + const serializer = ({ onError, onDone }: { onError(e: unknown): void; onDone(): void }) => { |
| 112 | + let pending = 0; |
| 113 | + let flushed = false; |
| 114 | + let finished = false; |
| 115 | + const settleWrite = () => { |
| 116 | + if (!flushed || pending || finished) return; |
| 117 | + finished = true; |
| 118 | + onDone(); |
| 119 | + }; |
| 120 | + return { |
| 121 | + write(_: string, value: any) { |
| 122 | + if (!value || typeof value.then !== "function") return; |
| 123 | + pending++; |
| 124 | + value.then( |
| 125 | + () => { |
| 126 | + onError(new Error("late")); |
| 127 | + pending--; |
| 128 | + settleWrite(); |
| 129 | + }, |
| 130 | + () => { |
| 131 | + pending--; |
| 132 | + settleWrite(); |
| 133 | + } |
| 134 | + ); |
| 135 | + }, |
| 136 | + flush() { |
| 137 | + flushed = true; |
| 138 | + settleWrite(); |
| 139 | + } |
| 140 | + }; |
| 141 | + }; |
| 142 | + let completed = false; |
| 143 | + let result: { state: string; html: string } | undefined; |
| 144 | + const escaped = await watchEscapes(async () => { |
| 145 | + result = await settle( |
| 146 | + renderToStream(App, { |
| 147 | + onError, |
| 148 | + serializer, |
| 149 | + onCompleteAll() { |
| 150 | + completed = true; |
| 151 | + } |
| 152 | + } as any) |
| 153 | + ); |
| 154 | + }); |
| 155 | + expect(escaped).toEqual([]); |
| 156 | + expect(result!.state).toBe("settled"); |
| 157 | + expect(result!.html).toContain("<main"); |
| 158 | + expect(completed).toBe(true); |
| 159 | + expect(heard).toEqual(["render/serialize"]); |
| 160 | + expect(ran).toEqual(["cleanup"]); |
| 161 | + }); |
| 162 | + |
| 163 | + test("without a hook anywhere the failure reaches console.error once", async () => { |
| 164 | + const logged: unknown[][] = []; |
| 165 | + vi.spyOn(console, "error").mockImplementation((...args: unknown[]) => { |
| 166 | + logged.push(args); |
| 167 | + }); |
| 168 | + let result: { state: string; html: string } | undefined; |
| 169 | + const escaped = await watchEscapes(async () => { |
| 170 | + result = await settle(renderToStream(App)); |
| 171 | + }); |
| 172 | + expect(escaped).toEqual([]); |
| 173 | + expect(result!.state).toBe("settled"); |
| 174 | + expect(result!.html).toBe(""); |
| 175 | + expect(logged.length).toBe(1); |
| 176 | + }); |
| 177 | +}); |
0 commit comments