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`
......
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