Unverified Commit 0e073366 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Reduce the number of hydration error tests (#46930)

`test/development/acceptance-app/hydration-error.test.ts` has a lot of tests because I ported all the tests in https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js. Running all those tests takes a lot of time, and to include all the different cases is overkill.

This PR removes most tests but keeps one test case per type of hydration error, and also one using suspense. This makes more sense, it should only test that all the different hydration error messages are picked up.
parent 8ca0233d
......@@ -436,7 +436,7 @@ export default function HotReload({
}, [dispatch])
const handleOnUnhandledError = useCallback((error: Error): void => {
// Component stack is added to the error in use-error-handler
// Component stack is added to the error in use-error-handler in case there was a hydration errror
const componentStack = (error as any)._componentStack
dispatch({
type: ACTION_UNHANDLED_ERROR,
......
export let hydrationErrorWarning: string | undefined
export let hydrationErrorComponentStack: string | undefined
// https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js used as a reference
const knownHydrationWarnings = new Set([
'Warning: Text content did not match. Server: "%s" Client: "%s"%s',
'Warning: Expected server HTML to contain a matching <%s> in <%s>.%s',
......@@ -9,6 +10,12 @@ const knownHydrationWarnings = new Set([
'Warning: Did not expect server HTML to contain the text node "%s" in <%s>.%s',
])
/**
* Patch console.error to capture hydration errors.
* If any of the knownHydrationWarnings are logged, store the message and component stack.
* When the hydration runtime error is thrown, the message and component stack are added to the error.
* This results in a more helpful error message in the error overlay.
*/
export function patchConsoleError() {
const prev = console.error
console.error = function (msg, serverContent, clientContent, componentStack) {
......
......@@ -64,10 +64,12 @@ if (typeof window !== 'undefined') {
)
) {
if (hydrationErrorWarning) {
// The patched console.error found hydration errors logged by React
// Append the logged warning to the error message
error.message += '\n\n' + hydrationErrorWarning
}
if (hydrationErrorComponentStack) {
// Component stack added to the error, picked up by the hot-reloader-client
// Hydration error component stack is added to the error, it's picked up by the hot-reloader-client
;(error as any)._componentStack = hydrationErrorComponentStack
}
error.message +=
......
......@@ -24,6 +24,8 @@ function onUnhandledError(ev: ErrorEvent) {
error.message.match(/(hydration|content does not match|did not match)/i)
) {
if (hydrationErrorWarning) {
// The patched console.error found hydration errors logged by React
// Append the logged warning to the error message
error.message += '\n\n' + hydrationErrorWarning
}
error.message += `\n\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error`
......
......@@ -3,7 +3,7 @@ import { sandbox } from './helpers'
import { createNextDescribe, FileRef } from 'e2e-utils'
import path from 'path'
// https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js
// https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js used as a reference
createNextDescribe(
'Error overlay for hydration errors',
{
......@@ -48,202 +48,8 @@ createNextDescribe(
await cleanup()
})
// TODO-APP: Decide if these logs should be displayed somewhere. They don't throw, just logs.
it.skip('should show correct hydration error when client and server render different html', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<main
className="child"
dangerouslySetInnerHTML={{
__html: isClient
? "<span>client</span>"
: "<span>server</span>",
}}
/>
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
// TODO-APP: Decide if these logs should be displayed somewhere. They don't throw, just logs.
it.skip('should show correct hydration error when client and server render different attributes', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<main
className={isClient ? "child client" : "child server"}
dir={isClient ? "ltr" : "rtl"}
/>
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
// TODO-APP: Decide if these logs should be displayed somewhere. They don't throw, just logs.
it.skip('should show correct hydration error when client renders extra attributes', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<main
className="child"
tabIndex={isClient ? 1 : null}
dir={isClient ? "ltr" : null}
/>
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
// TODO-APP: Decide if these logs should be displayed somewhere. They don't throw, just logs.
it.skip('should show correct hydration error when server renders extra attributes', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<main
className="child"
tabIndex={isClient ? null : 1}
dir={isClient ? null : "rtl"}
/>
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
// TODO-APP: Decide if these logs should be displayed somewhere. They don't throw, just logs.
it.skip('should show correct hydration error when both client and server render extra attributes', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<main
className="child"
tabIndex={isClient ? 1 : null}
dir={isClient ? null : "rtl"}
/>
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
// TODO-APP: Decide if these logs should be displayed somewhere. They don't throw, just logs.
it.skip('should show correct hydration error when client and server render different styles', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<main
className="child"
style={{
opacity: isClient ? 1 : 0,
}}
/>
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
it('should show correct hydration error when client renders an extra element as only child', async () => {
it('should show correct hydration error when client renders an extra element', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
......@@ -276,42 +82,7 @@ createNextDescribe(
await cleanup()
})
it('should show correct hydration error when client renders an extra element in the beginning', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{isClient && <header className="1" />}
<main className="2" />
<footer className="3" />
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <header> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when client renders an extra element in the middle', async () => {
it('should show correct hydration error when client renders an extra text node', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
......@@ -324,7 +95,7 @@ createNextDescribe(
return (
<div className="parent">
<header className="1" />
{isClient && <main className="2" />}
{isClient && "second"}
<footer className="3" />
</div>
);
......@@ -339,14 +110,15 @@ createNextDescribe(
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <main> in <div>.
Warning: Expected server HTML to contain a matching text node for \\"second\\" in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when client renders an extra element in the end', async () => {
it('should show correct hydration error when server renders an extra element', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
......@@ -358,9 +130,7 @@ createNextDescribe(
export default function Mismatch() {
return (
<div className="parent">
<header className="1" />
<main className="2" />
{isClient && <footer className="3" />}
{!isClient && <main className="only" />}
</div>
);
}
......@@ -374,43 +144,15 @@ createNextDescribe(
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <footer> in <div>.
Warning: Did not expect server HTML to contain a <main> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when client renders an extra text node as only child', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return <div className="parent">{isClient && "only"}</div>;
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Text content does not match server-rendered HTML.
Warning: Text content did not match. Server: \\"\\" Client: \\"only\\"
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when client renders an extra text node in the beginning 1', async () => {
it('should show correct hydration error when server renders an extra text node', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
......@@ -420,13 +162,7 @@ createNextDescribe(
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<header className="1" />
{isClient && "second"}
<footer className="3" />
</div>
);
return <div className="parent">{!isClient && "only"}</div>;
}
`,
],
......@@ -438,49 +174,15 @@ createNextDescribe(
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching text node for \\"second\\" in <div>.
Warning: Did not expect server HTML to contain the text node \\"only\\" in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when client renders an extra text node in the beginning 2', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{isClient && "first"}
<main className="2" />
<footer className="3" />
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching text node for \\"first\\" in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when client renders an extra text node in the end', async () => {
it('should show correct hydration error when client renders an extra node inside Suspense content', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
......@@ -488,465 +190,16 @@ createNextDescribe(
'app/page.js',
`
'use client'
import React from "react"
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<header className="1" />
<main className="2" />
{isClient && "third"}
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching text node for \\"third\\" in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra element as only child', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{!isClient && <main className="only" />}
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Did not expect server HTML to contain a <main> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra element in the beginning', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{!isClient && <header className="1" />}
<main className="2" />
<footer className="3" />
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <main> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra element in the middle', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<header className="1" />
{!isClient && <main className="2" />}
<footer className="3" />
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <footer> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra element in the end', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<header className="1" />
<main className="2" />
{!isClient && <footer className="3" />}
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Did not expect server HTML to contain a <footer> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra text node as only child', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return <div className="parent">{!isClient && "only"}</div>;
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Did not expect server HTML to contain the text node \\"only\\" in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra text node in the beginning', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{!isClient && "first"}
<main className="2" />
<footer className="3" />
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <main> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra text node in the middle', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<header className="1" />
{!isClient && "second"}
<footer className="3" />
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <footer> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra text node in the end', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<header className="1" />
<main className="2" />
{!isClient && "third"}
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Did not expect server HTML to contain the text node \\"third\\" in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it.skip('should show correct hydration error when client renders an extra Suspense node in content mode', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
import React from "react"
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{isClient && (
<React.Suspense fallback={<p>Loading...</p>}>
<main className="only" />
</React.Suspense>
)}
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
it('should show correct hydration error when server renders an extra Suspense node in content mode', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
import React from "react"
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{!isClient && (
<React.Suspense fallback={<p>Loading...</p>}>
<main className="only" />
</React.Suspense>
)}
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Did not expect server HTML to contain a <main> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it.skip('should show correct hydration error when client renders an extra Suspense node in fallback mode', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
import React from "react"
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{isClient && (
<React.Suspense fallback={<p>Loading...</p>}>
<main className="only" />
<Never />
</React.Suspense>
)}
</div>
);
}
function Never() {
throw new Promise((resolve) => {})
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
it.skip('should show correct hydration error when server renders an extra Suspense node in fallback mode', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
import React from "react"
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{!isClient && (
<React.Suspense fallback={<p>Loading...</p>}>
<main className="only" />
<Never />
</React.Suspense>
)}
</div>
);
}
function Never() {
throw new Promise((resolve) => {})
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
it('should show correct hydration error when client renders an extra node inside Suspense content', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
import React from "react"
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<React.Suspense fallback={<p>Loading...</p>}>
<header className="1" />
{isClient && <main className="second" />}
<footer className="3" />
</React.Suspense>
<React.Suspense fallback={<p>Loading...</p>}>
<header className="1" />
{isClient && <main className="second" />}
<footer className="3" />
</React.Suspense>
</div>
);
}
......@@ -965,314 +218,6 @@ createNextDescribe(
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra node inside Suspense content', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
import React from "react"
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<React.Suspense fallback={<p>Loading...</p>}>
<header className="1" />
{!isClient && <main className="second" />}
<footer className="3" />
</React.Suspense>
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <footer> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it.skip('should show correct hydration error when client renders an extra node inside Suspense fallback', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
import React from "react"
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<React.Suspense
fallback={
<>
<p>Loading...</p>
{isClient && <br />}
</>
}
>
<main className="only" />
</React.Suspense>
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
it.skip('should show correct hydration error when server renders an extra node inside Suspense fallback', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<React.Suspense
fallback={
<>
<p>Loading...</p>
{!isClient && <br />}
</>
}
>
<main className="only" />
<Never />
</React.Suspense>
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot()
await cleanup()
})
it('should show correct hydration error when client renders an extra Fragment node', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{isClient && (
<>
<header className="1" />
<main className="2" />
<footer className="3" />
</>
)}
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <header> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra Fragment node', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
{!isClient && (
<>
<header className="1" />
<main className="2" />
<footer className="3" />
</>
)}
</div>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Did not expect server HTML to contain a <header> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when client renders an extra node deeper in the tree', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return isClient ? <ProfileSettings /> : <MediaSettings />;
}
function ProfileSettings() {
return (
<div className="parent">
<input />
<Panel type="profile" />
</div>
);
}
function MediaSettings() {
return (
<div className="parent">
<input />
<Panel type="media" />
</div>
);
}
function Panel({ type }) {
return (
<>
<header className="1" />
<main className="2" />
{type === "profile" && <footer className="3" />}
</>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <footer> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
it('should show correct hydration error when server renders an extra node deeper in the tree', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return isClient ? <ProfileSettings /> : <MediaSettings />;
}
function ProfileSettings() {
return (
<div className="parent">
<input />
<Panel type="profile" />
</div>
);
}
function MediaSettings() {
return (
<div className="parent">
<input />
<Panel type="media" />
</div>
);
}
function Panel({ type }) {
return (
<>
<header className="1" />
<main className="2" />
{type !== "profile" && <footer className="3" />}
</>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(`
"Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Did not expect server HTML to contain a <footer> in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`)
await cleanup()
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment