Jest-styled-components: Does not work with enzyme

Created on 30 Sep 2019  ·  3Comments  ·  Source: styled-components/jest-styled-components

The following code produces a weird snapshot value.

Code:

import React from 'react';
import styled from 'styled-components';
import { shallow } from "enzyme";
import toJson from 'enzyme-to-json'

it('renders without crashing', () => {
  const Button = styled.div`
    color: red;
  `;

  const wrapper = shallow(<Button />);
  expect(toJson(wrapper)).toMatchSnapshot();
});

Snapshot:

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renders without crashing 1`] = `
<StyledComponent
  forwardedComponent={
    Object {
      "$$typeof": Symbol(react.forward_ref),
      "attrs": Array [],
      "componentStyle": ComponentStyle {
        "componentId": "sc-bdVaJa",
        "isStatic": false,
        "rules": Array [
          "
    color: red;
  ",
        ],
      },
      "displayName": "styled.div",
      "foldedComponentIds": Array [],
      "render": [Function],
      "styledComponentId": "sc-bdVaJa",
      "target": "div",
      "toString": [Function],
      "usesTheme": false,
      "warnTooManyClasses": [Function],
      "withComponent": [Function],
    }
  }
  forwardedRef={null}
/>
`;

Expected: (I get this if I try react-test-renderer directly instead of enzyme)

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renders without crashing 1`] = `
.c0 {
  color: red;
}

<div
  className="c0"
/>
`;

Versions:

    "enzyme": "^3.10.0",
    "enzyme-adapter-react-16": "^1.14.0",
    "enzyme-to-json": "^3.4.0",
    "jest-styled-components": "^6.3.3",
    "react": "^16.10.1",
    "react-dom": "^16.10.1",
    "react-scripts": "3.1.2",
    "styled-components": "^4.4.0"

Tried in a brand new project using create-react-app.

Debugging notes:

Most helpful comment

Any update on this, getting same issue.

Also this seems to be specific to shallow render as enzyme's render method works and import ShallowRenderer from 'react-test-renderer/shallow'; result in the same issue as enzyme's shallow

All 3 comments

Any update on this, getting same issue.

Also this seems to be specific to shallow render as enzyme's render method works and import ShallowRenderer from 'react-test-renderer/shallow'; result in the same issue as enzyme's shallow

@probablyup I think this should probably be higher priority, no?

so, I have been trying to get this working all day long .. just upgrade jest-styled-components to v7, then you will get an error, after searching around you also need styled-components v5 and everything is back to working 🤷‍♂️

hope it helps someone else

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrmartineau picture mrmartineau  ·  4Comments

leocreatini picture leocreatini  ·  4Comments

sumanbh picture sumanbh  ·  3Comments

santino picture santino  ·  5Comments

ghost picture ghost  ·  7Comments