|
| 1 | +// @ts-check |
| 2 | +const { test, expect } = require("@playwright/test"); |
| 3 | + |
| 4 | +const SERVER_URL = "http://localhost:7978"; |
| 5 | +const HYDRATION_MISMATCH_MESSAGE = "[HYDRATION MISMATCH]"; |
| 6 | +const HYDRATION_RECOVERY_MESSAGE = |
| 7 | + "Hydration mismatches detected. Falling back to a full client rebuild."; |
| 8 | + |
| 9 | +async function waitForBuild(request) { |
| 10 | + for (let i = 0; i < 30; i++) { |
| 11 | + const response = await request.get(SERVER_URL); |
| 12 | + const text = await response.text(); |
| 13 | + if (response.status() === 200 && text.includes('id="recovery-button"')) { |
| 14 | + return; |
| 15 | + } |
| 16 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 17 | + } |
| 18 | + |
| 19 | + throw new Error("Timed out waiting for the hydration recovery fixture to build"); |
| 20 | +} |
| 21 | + |
| 22 | +test("hydration mismatch recovers nested structure, text, attributes, and placeholders", async ({ |
| 23 | + page, |
| 24 | + request, |
| 25 | +}) => { |
| 26 | + await waitForBuild(request); |
| 27 | + |
| 28 | + const serverResponse = await request.get(SERVER_URL); |
| 29 | + expect(serverResponse.status()).toBe(200); |
| 30 | + |
| 31 | + const serverHtml = await serverResponse.text(); |
| 32 | + expect(serverHtml).toContain('id="recovery-button"'); |
| 33 | + expect(serverHtml).toContain("Server text content"); |
| 34 | + expect(serverHtml).toContain("Server placeholder content"); |
| 35 | + expect(serverHtml).not.toContain('role="status"'); |
| 36 | + expect(serverHtml).not.toContain('title="Client attribute title"'); |
| 37 | + |
| 38 | + const consoleMessages = []; |
| 39 | + const consoleErrors = []; |
| 40 | + const pageErrors = []; |
| 41 | + |
| 42 | + page.on("console", (msg) => { |
| 43 | + consoleMessages.push(msg.text()); |
| 44 | + if (msg.type() === "error") { |
| 45 | + consoleErrors.push(msg.text()); |
| 46 | + } |
| 47 | + }); |
| 48 | + page.on("pageerror", (error) => { |
| 49 | + pageErrors.push(error.message); |
| 50 | + }); |
| 51 | + |
| 52 | + await page.goto(SERVER_URL); |
| 53 | + await page.waitForLoadState("networkidle"); |
| 54 | + |
| 55 | + const mismatchMessages = () => |
| 56 | + consoleMessages.filter((message) => |
| 57 | + message.includes(HYDRATION_MISMATCH_MESSAGE), |
| 58 | + ); |
| 59 | + const hasMismatch = (...fragments) => |
| 60 | + mismatchMessages().some((message) => |
| 61 | + fragments.every((fragment) => message.includes(fragment)), |
| 62 | + ); |
| 63 | + |
| 64 | + await expect |
| 65 | + .poll(() => mismatchMessages().length, { |
| 66 | + message: "expected one warning for each mismatch class", |
| 67 | + }) |
| 68 | + .toBe(4); |
| 69 | + await expect |
| 70 | + .poll( |
| 71 | + () => |
| 72 | + consoleMessages.filter((message) => |
| 73 | + message.includes(HYDRATION_RECOVERY_MESSAGE), |
| 74 | + ).length, |
| 75 | + { message: "expected the hydration fallback warning to be logged once" }, |
| 76 | + ) |
| 77 | + .toBe(1); |
| 78 | + |
| 79 | + expect( |
| 80 | + mismatchMessages().every( |
| 81 | + (message) => |
| 82 | + message.includes("Reason:") && |
| 83 | + message.includes("--- expected") && |
| 84 | + message.includes("+++ actual") && |
| 85 | + message.includes("@@"), |
| 86 | + ), |
| 87 | + ).toBeTruthy(); |
| 88 | + |
| 89 | + expect( |
| 90 | + hasMismatch( |
| 91 | + "Reason: Expected <strong>, found <span>.", |
| 92 | + "-strong {", |
| 93 | + "+span {", |
| 94 | + ), |
| 95 | + ).toBeTruthy(); |
| 96 | + expect( |
| 97 | + hasMismatch( |
| 98 | + 'Reason: Expected text "Client text content", found text "Server text content".', |
| 99 | + '- "Client text content",', |
| 100 | + '+ "Server text content",', |
| 101 | + ), |
| 102 | + ).toBeTruthy(); |
| 103 | + expect( |
| 104 | + hasMismatch( |
| 105 | + "Reason: Expected <div> with attributes [role, title], but the DOM node is missing them.", |
| 106 | + 'role: "status"', |
| 107 | + 'title: "Client attribute title"', |
| 108 | + ), |
| 109 | + ).toBeTruthy(); |
| 110 | + expect( |
| 111 | + hasMismatch( |
| 112 | + "Reason: Expected placeholder (comment node), found node type 1.", |
| 113 | + "VNode::placeholder()", |
| 114 | + "+ p {", |
| 115 | + ), |
| 116 | + ).toBeTruthy(); |
| 117 | + |
| 118 | + const recoveryButton = page.locator("#recovery-button"); |
| 119 | + await expect(recoveryButton).toHaveCount(1); |
| 120 | + await expect(recoveryButton).toHaveText("Recovered 0"); |
| 121 | + |
| 122 | + const nestedLeaf = page.locator("#nested-leaf"); |
| 123 | + await expect(nestedLeaf).toHaveCount(1); |
| 124 | + await expect(nestedLeaf).toHaveJSProperty("tagName", "STRONG"); |
| 125 | + await expect(nestedLeaf).toHaveText("Nested client leaf"); |
| 126 | + |
| 127 | + const textMismatch = page.locator("#text-mismatch"); |
| 128 | + await expect(textMismatch).toHaveText("Client text content"); |
| 129 | + |
| 130 | + const attributeMismatch = page.locator("#attribute-mismatch"); |
| 131 | + await expect(attributeMismatch).toHaveAttribute("role", "status"); |
| 132 | + await expect(attributeMismatch).toHaveAttribute( |
| 133 | + "title", |
| 134 | + "Client attribute title", |
| 135 | + ); |
| 136 | + |
| 137 | + await expect(page.locator("#placeholder-mismatch-shell p")).toHaveCount(0); |
| 138 | + await expect(page.locator("body")).not.toContainText("Server text content"); |
| 139 | + await expect(page.locator("body")).not.toContainText( |
| 140 | + "Server placeholder content", |
| 141 | + ); |
| 142 | + |
| 143 | + await recoveryButton.click(); |
| 144 | + await expect(recoveryButton).toHaveText("Recovered 1"); |
| 145 | + await recoveryButton.click(); |
| 146 | + await expect(recoveryButton).toHaveText("Recovered 2"); |
| 147 | + |
| 148 | + expect(pageErrors).toEqual([]); |
| 149 | + expect(consoleErrors).toEqual([]); |
| 150 | +}); |
0 commit comments