-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retain component stacks when rethrowing an error that already generat…
…ed a component stack
- Loading branch information
Showing
3 changed files
with
128 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/react-reconciler/src/__tests__/ReactErrorStacks-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and 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 | ||
* @jest-environment node | ||
*/ | ||
'use strict'; | ||
|
||
let React; | ||
let ReactNoop; | ||
let Scheduler; | ||
|
||
describe('ReactFragment', () => { | ||
beforeEach(function() { | ||
jest.resetModules(); | ||
|
||
React = require('react'); | ||
ReactNoop = require('react-noop-renderer'); | ||
Scheduler = require('scheduler'); | ||
}); | ||
|
||
function componentStack(components) { | ||
return components | ||
.map(component => `\n in ${component} (at **)`) | ||
.join(''); | ||
} | ||
|
||
function normalizeCodeLocInfo(str) { | ||
return ( | ||
str && | ||
str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function(m, name) { | ||
return '\n in ' + name + ' (at **)'; | ||
}) | ||
); | ||
} | ||
|
||
it('retains component stacks when rethrowing an error', () => { | ||
function Foo() { | ||
return ( | ||
<RethrowingBoundary> | ||
<Bar /> | ||
</RethrowingBoundary> | ||
); | ||
} | ||
function Bar() { | ||
return <SomethingThatErrors />; | ||
} | ||
function SomethingThatErrors() { | ||
throw new Error('uh oh'); | ||
} | ||
|
||
class RethrowingBoundary extends React.Component { | ||
static getDerivedStateFromError(error) { | ||
throw error; | ||
} | ||
|
||
render() { | ||
return this.props.children; | ||
} | ||
} | ||
|
||
const errors = []; | ||
class CatchingBoundary extends React.Component { | ||
constructor() { | ||
super(); | ||
this.state = {}; | ||
} | ||
static getDerivedStateFromError(error) { | ||
return {errored: true}; | ||
} | ||
componentDidCatch(err, errInfo) { | ||
errors.push(err.message, normalizeCodeLocInfo(errInfo.componentStack)); | ||
} | ||
render() { | ||
if (this.state.errored) { | ||
return null; | ||
} | ||
return this.props.children; | ||
} | ||
} | ||
|
||
ReactNoop.render( | ||
<CatchingBoundary> | ||
<Foo /> | ||
</CatchingBoundary>, | ||
); | ||
expect(Scheduler).toFlushWithoutYielding(); | ||
expect(errors).toEqual([ | ||
'uh oh', | ||
componentStack([ | ||
'SomethingThatErrors', | ||
'Bar', | ||
'RethrowingBoundary', | ||
'Foo', | ||
'CatchingBoundary', | ||
]), | ||
]); | ||
}); | ||
}); |