-
Notifications
You must be signed in to change notification settings - Fork 48.8k
Replace React.error and React.warn with getComponentStack #16017
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,125 @@ | ||
/** | ||
* 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'; | ||
|
||
function normalizeCodeLocInfo(str) { | ||
return str && str.replace(/at .+?:\d+/g, 'at **'); | ||
} | ||
|
||
describe('getComponentStack', () => { | ||
let React = null; | ||
let ReactTestRenderer = null; | ||
let getComponentStack = null; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
|
||
React = require('react'); | ||
ReactTestRenderer = require('react-test-renderer'); | ||
|
||
getComponentStack = React.getComponentStack; | ||
}); | ||
|
||
if (!__DEV__) { | ||
it('does nothing in production mode', () => { | ||
expect(getComponentStack()).toBe(''); | ||
}); | ||
} | ||
|
||
if (__DEV__) { | ||
it('returns an empty component stack when called outside of render', () => { | ||
expect(getComponentStack()).toBe(''); | ||
}); | ||
|
||
it('includes component stack when called from a render method', () => { | ||
class Parent extends React.Component { | ||
render() { | ||
return <Child />; | ||
} | ||
} | ||
|
||
let capturedComponentStack = null; | ||
function Child() { | ||
capturedComponentStack = getComponentStack(); | ||
return null; | ||
} | ||
|
||
ReactTestRenderer.create(<Parent />); | ||
expect(normalizeCodeLocInfo(capturedComponentStack)).toBe( | ||
'\n in Child (at **)' + '\n in Parent (at **)', | ||
); | ||
}); | ||
|
||
it('returns component stack when called from a render phase lifecycle method', () => { | ||
function Parent() { | ||
return <Child />; | ||
} | ||
|
||
let capturedComponentStack = null; | ||
class Child extends React.Component { | ||
UNSAFE_componentWillMount() { | ||
capturedComponentStack = getComponentStack(); | ||
} | ||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
ReactTestRenderer.create(<Parent />); | ||
expect(normalizeCodeLocInfo(capturedComponentStack)).toBe( | ||
'\n in Child (at **)' + '\n in Parent (at **)', | ||
); | ||
}); | ||
|
||
it('returns component stack when called from a commit phase lifecycle method', () => { | ||
function Parent() { | ||
return <Child />; | ||
} | ||
|
||
let capturedComponentStack = null; | ||
class Child extends React.Component { | ||
componentDidMount() { | ||
capturedComponentStack = getComponentStack(); | ||
} | ||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
ReactTestRenderer.create(<Parent />); | ||
expect(normalizeCodeLocInfo(capturedComponentStack)).toBe( | ||
'\n in Child (at **)' + '\n in Parent (at **)', | ||
); | ||
}); | ||
|
||
it('returns component stack when called from a passive effect handler', () => { | ||
class Parent extends React.Component { | ||
render() { | ||
return <Child />; | ||
} | ||
} | ||
|
||
let capturedComponentStack = null; | ||
function Child() { | ||
React.useEffect(() => { | ||
capturedComponentStack = getComponentStack(); | ||
}); | ||
return null; | ||
} | ||
|
||
ReactTestRenderer.act(() => { | ||
ReactTestRenderer.create(<Parent />); | ||
}); | ||
expect(normalizeCodeLocInfo(capturedComponentStack)).toBe( | ||
'\n in Child (at **)' + '\n in Parent (at **)', | ||
); | ||
}); | ||
} | ||
}); |
188 changes: 0 additions & 188 deletions
188
packages/react/src/__tests__/withComponentStack-test.js
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,22 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
import ReactSharedInternals from 'shared/ReactSharedInternals'; | ||
|
||
let getComponentStack = function() { | ||
return ''; | ||
}; | ||
|
||
if (__DEV__) { | ||
const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame; | ||
|
||
getComponentStack = function() { | ||
return ReactDebugCurrentFrame.getStackAddendum(); | ||
}; | ||
} | ||
|
||
export default getComponentStack; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this not return anything? Seemed weird to return void/undefined in prod and an empty string when called outside of the render phase in DEV, so I opted to return empty string in both cases for now.