Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot is missing css/styles when using shallow from enzyme #145

Closed
chhuang opened this issue May 29, 2018 · 6 comments
Closed

Snapshot is missing css/styles when using shallow from enzyme #145

chhuang opened this issue May 29, 2018 · 6 comments
Labels

Comments

@chhuang
Copy link

chhuang commented May 29, 2018

When I do:

import { shallow } from 'enzyme'

test('it works', () => {
  const wrapper = shallow(<Button />)
  expect(wrapper).toMatchSnapshot()
})

I only get:

exports[`Button renders correctly Renders correctly as a link 1`] = `<Button____Button />`;

It's fine if I use renderer from react-test-renderer:

const wrapper = renderer.create(<Button />).toJSON()
expect(wrapper).toMatchSnapshot()
@MicheleBertoli
Copy link
Member

Hello @chhuang , can you please provide more information about your Button component?

When I run the following code:

const Button = styled.button`
  color: red;
`

test('it works', () => {
  const wrapper = shallow(<Button />)
  expect(wrapper).toMatchSnapshot()
})

My snapshot looks like this (as expected):

exports[`it works 1`] = `
.c0 {
  color: red;
}

<button
  className="c0"
/>
`;

Thank you very much!

@carloscheddar
Copy link

carloscheddar commented Jun 6, 2018

I'm experiencing this issue as well and managed to reproduce it on code sandbox. I added 2 test cases 1 for enzyme and another for react-test-renderer. You can see the difference in the snapshot results on the test output https://codesandbox.io/s/o5ko8vlp39.

Edit:
The codesandbox specs seem to have been broken but you can see the output here:

Code

//Hello.js
import React from 'react';
import styled from 'styled-components';

const Button = styled.button`
  color: red;
  padding: 10px;
`;

class Hello extends React.Component {
  render() {
    return (
      <div>
        <h1>Hello {this.props.name}!</h1>
        <Button>Test</Button>
      </div>
    );
  }
}

export default Hello;

//Hello.test.js
import Adapter from "enzyme-adapter-react-16";
import React from "react";
import renderer from "react-test-renderer";
import { configure } from "enzyme";
import { shallow } from "enzyme";
import toJson from "enzyme-to-json";
import Hello from "./Hello";
import "jest-styled-components";

configure({ adapter: new Adapter() });

describe("<Hello /> enzyme snapshot", () => {
  it("serializes the styles", () => {
    const output = shallow(<Hello name="Test" />);
    expect(toJson(output)).toMatchSnapshot();
  });
});

describe("<Hello /> test renderer snapshot", () => {
  it("serializes the styles", () => {
    const output = renderer.create(<Hello name="Test" />);
    expect(output).toMatchSnapshot();
  });
});

Snapshots

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

exports[`<Hello /> enzyme snapshot serializes the styles 1`] = `
<div>
  <h1>
    Hello 
    Test
    !
  </h1>
  <Button>
    Test
  </Button>
</div>
`;

exports[`<Hello /> test renderer snapshot serializes the styles 1`] = `
.c0 {
  color: red;
  padding: 10px;
}

<div>
  <h1>
    Hello 
    Test
    !
  </h1>
  <button
    className="c0"
  >
    Test
  </button>
</div>
`;

@MicheleBertoli
Copy link
Member

Thank you very much @carloscheddar for providing detailed repro code.

The reasons why there's no style in the first snapshot are:

  • Hello is not a styled-component
  • you are shallow rendering

That's the expected behaviour.

If you try using mount you'll see the styles in the snapshot.

@rjohnep
Copy link

rjohnep commented Dec 4, 2018

Hi @MicheleBertoli
I have a similar problem with snapshots.
react-test-renderer generate right snapshots unlike enzyme

//react-test-renderer
.c0 {
  color: red;
}

<button
  className="c0"
/>


//****** VS ******//


//enzyme && enzyme-to-json
<StyledComponent
  forwardedComponent={
    Object {
      "$$typeof": Symbol(react.forward_ref),
      "attrs": Array [],
      "componentStyle": ComponentStyle {
        "componentId": "sc-bdVaJa",
        "isStatic": false,
        "rules": Array [
          "
  color: red;
",
        ],
      },
      "displayName": "styled.button",
      "foldedComponentIds": Array [],
      "render": [Function],
      "styledComponentId": "sc-bdVaJa",
      "target": "button",
      "toString": [Function],
      "warnTooManyClasses": [Function],
      "withComponent": [Function],
    }
  }
  forwardedRef={null}
/>

Can you check it?
In relation mount - no problem.
https://codesandbox.io/s/9jxq3j39yp?module=%2Fsrc%2Findex.test.js&previewwindow=tests

Regards

@MicheleBertoli
Copy link
Member

Thanks for your comment, @rjohnep.
Styled Components v4 is not fully supported yet (especially with Enzyme).

@Jose-Bustamante
Copy link

Jose-Bustamante commented Feb 11, 2019

Hi @MicheleBertoli is there any update when this could be fixed?

This is how my snapshot looks like when using Shallow, I am not sure if I could be doing something wrong.
Styled-components v: 4.1.1

       <styles__StyledCartTotalBody>
         <styles__OrderValue>
           <P>
             <Connect(branch(TranslationTextComponent))
               defaultMessage="Order value ex. VAT"
               id="checkout.orderTotalExclTax"
             />
           </P>
           <styles__Price>
              20
           </styles__Price>
         </styles__OrderValue>
       </styles__StyledCartTotalBody>
     </styles__StyledCartTotal>

import renderer from 'react-test-renderer';
works fine, also enzyme mount, is just a bit annoying to mock the store the whole time 😣

Could we re-open it? the issue don't seems to be solved.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants