import { createSandbox } from 'development-sandbox' import { FileRef, nextTestSetup } from 'e2e-utils' import path from 'path' import { outdent } from 'outdent' import { getRedboxTotalErrorCount, retry } from 'next-test-utils' const isReact18 = parseInt(process.env.NEXT_TEST_REACT_VERSION) === 18 // https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js used as a reference describe('Error overlay for hydration errors in Pages router', () => { const { next } = nextTestSetup({ files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), skipStart: true, }) it('includes a React docs link when hydration error does occur', async () => { await using sandbox = await createSandbox( next, new Map([ [ 'index.js', outdent` const isClient = typeof window !== 'undefined' export default function Mismatch() { return (
{isClient ? "client" : "server"}
); } `, ], ]), '/', { pushErrorAsConsoleLog: true } ) const { browser } = sandbox const logs = await browser.log() expect(logs).toEqual( expect.arrayContaining([ { message: isReact18 ? // React 18 has no link in the hydration message expect.stringContaining('Warning: Text content did not match.') : // TODO: Should probably link to https://nextjs.org/docs/messages/react-hydration-error instead. expect.stringContaining( 'https://react.dev/link/hydration-mismatch' ), source: 'error', }, ]) ) }) it('should show correct hydration error when client and server render different text', async () => { await using sandbox = await createSandbox( next, new Map([ [ 'index.js', outdent` const isClient = typeof window !== 'undefined' export default function Mismatch() { return (
{isClient ? "client" : "server"}
); } `, ], ]) ) const { session, browser } = sandbox // Pages Router uses React version without Owner Stacks hence the empty `stack` if (isReact18) { await expect(browser).toDisplayRedbox(` [ { "componentStack": "
+ "server" - "client"", "description": "Text content did not match. Server: "server" Client: "client"", "environmentLabel": null, "label": "Recoverable Error", "source": null, "stack": [], }, { "description": "There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.", "environmentLabel": null, "label": "Recoverable Error", "source": null, "stack": [], }, ] `) } else { await expect(browser).toDisplayRedbox(` { "componentStack": "...
+ client - server ...", "description": "Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used:", "environmentLabel": null, "label": "Recoverable Error", "source": "index.js (5:9) @ Mismatch > 5 |
{isClient ? "client" : "server"}
| ^", "stack": [ "main ", "Mismatch index.js (5:9)", ], } `) } await session.patch( 'index.js', outdent` export default function Mismatch() { return (
Value
); } ` ) await session.assertNoRedbox() expect(await browser.elementByCss('.child').text()).toBe('Value') }) it('should show correct hydration error when client renders an extra element', async () => { await using sandbox = await createSandbox( next, new Map([ [ 'index.js', outdent` const isClient = typeof window !== 'undefined' export default function Mismatch() { return (
{isClient &&
}
); } `, ], ]) ) const { browser } = sandbox await retry(async () => { expect(await getRedboxTotalErrorCount(browser)).toBe(isReact18 ? 3 : 1) }) if (isReact18) { await expect(browser).toDisplayRedbox(` [ { "componentStack": " >
>
", "description": "Expected server HTML to contain a matching
in
.", "environmentLabel": null, "label": "Runtime Error", "source": null, "stack": [], }, { "componentStack": " >
>
", "description": "Expected server HTML to contain a matching
in
.", "environmentLabel": null, "label": "Recoverable Error", "source": null, "stack": [], }, { "description": "There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.", "environmentLabel": null, "label": "Recoverable Error", "source": null, "stack": [], }, ] `) } else { await expect(browser).toDisplayRedbox(` { "componentStack": "...
+
...", "description": "Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used:", "environmentLabel": null, "label": "Recoverable Error", "source": "index.js (5:20) @ Mismatch > 5 | {isClient &&
} | ^", "stack": [ "main ", "Mismatch index.js (5:20)", ], } `) } }) it('should show correct hydration error when client renders an extra text node', async () => { await using sandbox = await createSandbox( next, new Map([ [ 'index.js', outdent` const isClient = typeof window !== 'undefined' export default function Mismatch() { return (
{isClient && "second"}
); } `, ], ]) ) const { browser } = sandbox await retry(async () => { expect(await getRedboxTotalErrorCount(browser)).toBe(isReact18 ? 3 : 1) }) if (isReact18) { await expect(browser).toDisplayRedbox(` [ { "componentStack": "
>
> "second"", "description": "Expected server HTML to contain a matching text node for "second" in
.", "environmentLabel": null, "label": "Runtime Error", "source": null, "stack": [], }, { "componentStack": "
>
> "second"", "description": "Expected server HTML to contain a matching text node for "second" in
.", "environmentLabel": null, "label": "Recoverable Error", "source": null, "stack": [], }, { "description": "There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.", "environmentLabel": null, "label": "Recoverable Error", "source": null, "stack": [], }, ] `) } else { await expect(browser).toDisplayRedbox(` { "componentStack": "...
+ second -