/* eslint-env jest */
import { createSandbox } from 'development-sandbox'
import { FileRef, nextTestSetup } from 'e2e-utils'
import path from 'path'
import { outdent } from 'outdent'
import { getToastErrorCount, retry } from 'next-test-utils'
describe('Error overlay for hydration errors in App router', () => {
const { next, isTurbopack } = 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([
[
'app/page.js',
outdent`
'use client'
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([
{
// TODO: Should probably link to https://nextjs.org/docs/messages/react-hydration-error instead.
message: 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([
[
'app/page.js',
outdent`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
{isClient ? "client" : "server"}
);
}
`,
],
])
)
const { session, browser } = sandbox
await expect(browser).toDisplayCollapsedRedbox(`
{
"componentStack": "...
} forbidden={undefined} unauthorized={undefined}>
} forbidden={undefined} ...>
+ client
- server
...
...
...",
"description": "Hydration failed because the server rendered text 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": "app/page.js (6:7) @ Mismatch
> 6 | {isClient ? "client" : "server"}
| ^",
"stack": [
"main ",
"Mismatch app/page.js (6:7)",
],
}
`)
expect(await session.getRedboxErrorLink()).toMatchInlineSnapshot(
`"See more info here: https://nextjs.org/docs/messages/react-hydration-error"`
)
await session.patch(
'app/page.js',
outdent`
'use client'
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([
[
'app/page.js',
outdent`
'use client'
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
{isClient && }
);
}
`,
],
])
)
const { browser } = sandbox
await expect(browser).toDisplayCollapsedRedbox(`
{
"componentStack": "...
} forbidden={undefined} unauthorized={undefined}>
} forbidden={undefined} ...>
+
...
...
...",
"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": "app/page.js (6:20) @ Mismatch
> 6 | {isClient && }
| ^",
"stack": [
"main ",
"Mismatch app/page.js (6:20)",
],
}
`)
})
it('should show correct hydration error when extra attributes set on server', async () => {
await using sandbox = await createSandbox(
next,
new Map([
[
'app/layout.js',
outdent`
'use client'
const isServer = typeof window === 'undefined'
export default function Root({ children }) {
return (
{children}
)
}
`,
],
['app/page.js', `export default function Page() { return 'page' }`],
])
)
const { browser } = sandbox
if (isTurbopack) {
await expect(browser).toDisplayCollapsedRedbox(`
{
"componentStack": "...
}>
} ...>