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.

Missing displayName for styled-component 4.x and enzyme-to-json

See original GitHub issue

For latest styled-component v4 and latest enzyme version, in test cases, if we pass the Component Instance on props, when make snapshots by enzyme-to-json and enzyme-adapter-react-16, the displayName will missing.

For the styled-component 3.x version, it works well for same enzyme version. Only happens for the new styled-component 4.x version.

There is a relevant issue of styled-component v4, https://github.com/styled-components/styled-components/issues/1985

Please help check and give some proper fixing, thanks!

Used enzyme@3.9.0, styled-components@4.2, enzyme-adapter-react-16@1.12.1, enzynme-to-json@3.3.5, and setup "enzyme-to-json/serializer" for snapshotSerializers.

Here are the code sample

it('render small app', () => {
        const result = shallow(
            <SmallApp
                company="Supercell"
                i18n={mockI18n}
            />
        );
        expect(result).toMatchSnapshot();
    });

The simple version of SmallApp is

class SmallApp extends React.PureComponent<{
    name?: string | React.Node,
}> {
    render() {
        const {
            name,

        } = this.props;


        let nameEl;
        if (name == null) {
            nameEl = <div />;
        } else {
            nameEl = (
                <Text ellipsis style={{ color: colors.brand.bigStone }}>
                    {name}
                </Text>
            );
        }
        return (
            <SmallEntity name={nameEl}/>
        );
    }
}

The Text component is styled component,

const Text = styled.div`
    color: inherit;
    text-overflow: ${(props: TextProps) => (props.ellipsis ? 'ellipsis' : 'inherit')};
    overflow: ${(props: TextProps) => (props.ellipsis ? 'hidden' : 'inherit')};
`;

Current behavior

Shallow or mount styled-component Component would get <ForwardRef > and <StyledComponent> instead of its correct displayName.

<SmallEntity
  company="Supercell"
  name={
    <ForwardRef
      style={
        Object {
      }
    >
      Supercell
    </ForwardRef>
  }
/>

Expected behavior

Should get correct displayName in snapshots, not the ForwardRef, expected behavior should be like this, name={<Text ...

<SmallEntity
  company="Supercell"
  name={
    <Text
      style={
        Object {
      }
    >
      Supercell
    </Text>
  }
/>

Your environment

Jest+Enzyme+enzyme-to-json+styled-components

Used enzyme@3.9.0, styled-components@4.2, enzyme-adapter-react-16@1.12.1, enzynme-to-json@3.3.5, and setup "enzyme-to-json/serializer" for snapshotSerializers.

API

  • shallow
  • mount

Version

library version
enzyme 3.8.0
react 16.7.0
react-dom 16.7.0
react-test-renderer
adapter (below)

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-16.3
  • enzyme-adapter-react-16.2
  • enzyme-adapter-react-16.1
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
xunuoicommented, Apr 29, 2019

Seems this should be the bug of styled-component v4, I created one fixing PR for this, https://github.com/styled-components/styled-components/pull/2516

Still appreciate the enzyme team would have a double check for this issue, if nothing about enzyme, we should close this issue? Thanks ^ ^

0reactions
ljharbcommented, Dec 23, 2019

@XxX-MLGNoob-XxX you’re using enzyme’s debug, which shows the actual component tree - which in this case, due to styled-components’ implementation, includes all those extra divs and components. In other words, .debug() is not meant to be some kind of snapshot of the jsx you typed, it’s a formatted version of the actual react tree.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing displayName for styled-component 4.x and enzyme-to ...
For latest styled-component v4 and latest enzyme version, in test cases, if we pass the Component Instance on props, when make snapshots by...
Read more >
reactjs - Component definition is missing display name react ...
Exporting an arrow function directly doesn't give the component a displayName , but if you export a regular function the function name will ......
Read more >
You're missing a trick with your styled-components in React
Just after your styled-component declaration, set a string value for the display name attribute on your component: const CustomButton = styled.button`
Read more >
@emotion/jest | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
React Archives - Page 7 of 12 - The Branding Store
It's always a good practice to validate the data we get as props by using ... module.exports = { styledComponents: { displayName: true,...
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