Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing test for recoverable error when new, unrelated Suspense boundaries commit #32436

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,111 @@ describe('ReactDOMFizzServer', () => {
expect(loggedErrors).toEqual([theError]);
});

it('Errors in boundaries should not be logged when new, unrelated Suspense boundaries commit', async () => {
class ErrorBoundary extends React.Component {
state = {error: null};
static getDerivedStateFromError(error) {
return {error};
}

render() {
if (this.state.error) {
return <Text text="did error" />;
}
return this.props.children;
}
}

const theError = new Error('Boom');
function Page() {
throw theError;
}

let renderSuspense;
function Sibling() {
const [state, setState] = React.useState(false);
renderSuspense = () => setState(true);

return state ? <Suspense /> : null;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new Suspense boundary triggers the error. If we start out with return <Suspense /> no error is triggered.

}

function App({isClient}) {
return (
<>
<ErrorBoundary>
<Suspense>
<Page />
</Suspense>
</ErrorBoundary>
<Sibling />
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're triggering an update in the <Sibling> tree long after <ErrorBoundary> committed yet we still get the "switched to client-side rendering" from the <ErrorBoundary> tree.

</>
);
}

const ssrErrors = [];
function onError(x) {
ssrErrors.push(x);
return 'hash(' + x.message + ')';
}
const expectedDigest = onError(theError);
ssrErrors.length = 0;

await act(() => {
const {pipe} = renderToPipeableStream(
<App />,

{
onError,
},
);
pipe(writable);
});
expect(ssrErrors).toEqual([theError]);

const errors = [];
ReactDOMClient.hydrateRoot(container, <App />, {
onRecoverableError(error, errorInfo) {
errors.push({error, errorInfo});
},
});
await waitForAll([]);

expect(getVisibleChildren(container)).toEqual('did error');
expectErrors(errors, [], []);

renderSuspense();
await waitForAll([]);

expect(getVisibleChildren(container)).toEqual('did error');
expectErrors(
errors,
[
[
'Switched to client rendering because the server rendering errored:\n\n' +
theError.message,
expectedDigest,
componentStack(['Page', 'Suspense', 'ErrorBoundary', 'App']),
],
[
'Switched to client rendering because the server rendering errored:\n\n' +
theError.message,
expectedDigest,
componentStack(['Page', 'Suspense', 'ErrorBoundary', 'App']),
],
],
[
[
'The server could not finish this Suspense boundary, likely due to an error during server rendering. Switched to client rendering.',
expectedDigest,
],
[
'The server could not finish this Suspense boundary, likely due to an error during server rendering. Switched to client rendering.',
expectedDigest,
],
],
);
});

it('should asynchronously load the suspense boundary', async () => {
await act(() => {
const {pipe} = renderToPipeableStream(
Expand Down
Loading