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.

Snapshot tests showing components as '<ForwardRef />' instead of component name

See original GitHub issue
  • emotion version:
"@emotion/core": "^10.0.6",
"@emotion/native": "^10.0.6",
  • react version:
"react": "16.6.3",
"react-native": "0.57.8",

Relevant code:

const StyledText = styled.Text<StyledTextProps>`
  ${space}
  ${textType}
  ${textAlign}
  ${textColor}
`;

StyledText.defaultProps = {
  type: 'default',
};

StyledText.displayName = 'StyledText';

What you did: Used the StyledText component I just created in another component and did a snapshot test.

What happened: I expected the name in the snapshots to show up as <StyledText ...> but it showed up as <ForwardRef ...> instead.

Suggested solution: The displayName should be honoured when created styled components like this, or at least there should be an API to provide that name to emotion.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:18 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
jooj123commented, Mar 13, 2019

Got around this by not applying @emotion/babel-preset-css-prop specifically for test babel config, eg:

process.env.NODE_ENV === 'test'
        ? null
        : [
            // see: https://emotion.sh/docs/@emotion/babel-preset-css-prop
            require.resolve('@emotion/babel-preset-css-prop'),
            {
              sourceMap: true,
              autoLabel: false,
              cssPropOptimization: true,
            },
          ],

But yeah maybe not the best way - ideally should work out of the box

1reaction
danieldelcorecommented, Mar 24, 2019

Hi all, this might be have been fixed in this pr: https://github.com/emotion-js/emotion/pull/1280 We just merged it, so it might be a couple of days until it’s released.

@mitchellhamilton how often do you cut new releases?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ForwardRef component error: jest react native - Stack Overflow
Here is What I have done to resolve this problem: Just add this function : jest.mock("react", () => { const originReact = jest....
Read more >
What does forwardRef by a component in React dev tools ...
The forwardRef calls aren't in your own code, they are in the packages that you are using. styled.button appears to be from styled-components...
Read more >
Continuous integration for React applications using Jest and ...
I recommend using snapshot testing to track changes to components. In this method, you take snapshots of components and when the component's ......
Read more >
Use with React - Styletron
Since styled is technically a higher-order component, we need to set the displayName explicitly to see the real component name when debugging (or...
Read more >
Snapshot Testing - Jest
A typical snapshot test case renders a UI component, ... A similar approach can be taken when it comes to testing your React...
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