From 1c2c0383b8a094dbde7a456f28dc11a834aa378b Mon Sep 17 00:00:00 2001 From: eps1lon Date: Mon, 12 Oct 2020 19:32:24 +0200 Subject: [PATCH 1/2] test: Add regression test for hooks after error boundaries --- ...ReactErrorBoundariesHooks-test.internal.js | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 packages/react-dom/src/__tests__/ReactErrorBoundariesHooks-test.internal.js diff --git a/packages/react-dom/src/__tests__/ReactErrorBoundariesHooks-test.internal.js b/packages/react-dom/src/__tests__/ReactErrorBoundariesHooks-test.internal.js new file mode 100644 index 00000000000..635066e5b0e --- /dev/null +++ b/packages/react-dom/src/__tests__/ReactErrorBoundariesHooks-test.internal.js @@ -0,0 +1,67 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @emails react-core + */ + +'use strict'; + +let React; +let ReactDOM; + +describe('ReactErrorBoundariesHooks', () => { + beforeEach(() => { + jest.resetModules(); + ReactDOM = require('react-dom'); + React = require('react'); + }); + + it('should preserve hook order if errors are caught', () => { + function ErrorThrower() { + React.useMemo(() => undefined, []); + throw new Error('expected'); + return null; + } + + function StatefulComponent() { + React.useState(null); + return ' | stateful'; + } + + class ErrorHandler extends React.Component { + state = {error: null}; + + componentDidCatch(error) { + return this.setState({error}); + } + + render() { + if (this.state.error !== null) { + return

Handled error: {this.state.error.message}

; + } + return this.props.children; + } + } + + function App(props) { + return ( + + + + + + + ); + } + + const container = document.createElement('div'); + ReactDOM.render(, container); + + expect(() => { + ReactDOM.render(, container); + }).not.toThrow(); + }); +}); From e5972d6fca3d406ac8f426703dd0edd5f1e4a05d Mon Sep 17 00:00:00 2001 From: eps1lon Date: Mon, 12 Oct 2020 19:57:34 +0200 Subject: [PATCH 2/2] fix lint --- .../src/__tests__/ReactErrorBoundariesHooks-test.internal.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/react-dom/src/__tests__/ReactErrorBoundariesHooks-test.internal.js b/packages/react-dom/src/__tests__/ReactErrorBoundariesHooks-test.internal.js index 635066e5b0e..078c58e0357 100644 --- a/packages/react-dom/src/__tests__/ReactErrorBoundariesHooks-test.internal.js +++ b/packages/react-dom/src/__tests__/ReactErrorBoundariesHooks-test.internal.js @@ -23,7 +23,6 @@ describe('ReactErrorBoundariesHooks', () => { function ErrorThrower() { React.useMemo(() => undefined, []); throw new Error('expected'); - return null; } function StatefulComponent() {