In tests, stateless components dont have displayName
See original GitHub issueIs this a bug report?
Not sure if it’s CRA configuration issue or Jest bug.
Can you also reproduce the problem with npm 4.x?
Yes (node v7, npm v4).
Which terms did you search for in User Guide?
anonymous, displayname (also searched from issues).
Environment
node -v
: v8.4.0npm -v
: 5.3.0yarn --version
: 0.27.5npm ls react-scripts
: 1.0.12
Then, specify:
- Operating system: Ubuntu 17.04
- Browser and version (if relevant):
Steps to Reproduce
-
Fresh install of CRA
-
Add a stateless component like this (
Hello.js
)
import React from 'react'
import PropTypes from 'prop-types'
export const Hello = ({name}) => (<div>Hi {name}</div>)
Hello.propTypes = {
name: PropTypes.node.isRequired,
}
export default Hello
- Add a test (
Hello.spec.js
)
import React from 'react';
import ReactDOM from 'react-dom';
import Hello from './Hello'
describe('<Hello />', () => {
it('renders', () => {
const div = document.createElement('div');
ReactDOM.render(<Hello />, div);
})
})
- Run tests,
yarn test
Expected Behavior
PropTypes error should show a component name, Hello
, like development build does (yarn start
).
Actual Behavior
Warning doesnt have a component name, shows <<anonymous>>
instead:
console.error node_modules/fbjs/lib/warning.js:35
Warning: Failed prop type: The prop `name` is marked as required in `<<anonymous>>`, but its value is `undefined`.
in Unknown (at Hello.spec.js:8)
Reproducible Demo
Copy & paste snippets from repro steps.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
how to set displayName in a functional component [React]
The displayName string is used in debugging messages. Usually, you don't need to set it explicitly because it's inferred from the name of...
Read more >[Solved]-Find component by display name when the ...
Coding example for the question Find component by display name when the component is stateless functional, with Enzyme-Reactjs.
Read more >How 'displayName' Helps In Testing React Using Styled ...
We will start with the code which does not contain displayName to understand the issues. The main component is user.
Read more >Test styled components in React efficiently using 'displayName'
We will start with the code which does not contain displayName to understand the issues. The main component is user.
Read more >React.js displayName - GeeksforGeeks
It's usually not necessary to set it explicitly because the name of the function or class that describes the component infers it. If...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sounds like probably https://github.com/babel/babel/issues/5630
Seems like both referenced PRs got merged so eventually fixes will propagate.