question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In tests, stateless components dont have displayName

See original GitHub issue

Is 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

  1. node -v: v8.4.0
  2. npm -v: 5.3.0
  3. yarn --version: 0.27.5
  4. npm ls react-scripts: 1.0.12

Then, specify:

  1. Operating system: Ubuntu 17.04
  2. Browser and version (if relevant):

Steps to Reproduce

  1. Fresh install of CRA

  2. 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
  1. 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);
  })
})
  1. 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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
loganfsmythcommented, Sep 1, 2017
0reactions
gaearoncommented, Jan 8, 2018

Seems like both referenced PRs got merged so eventually fixes will propagate.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found