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

Doesn't seem to support Jest 28 and newest #429

Closed
buugaaga opened this issue Apr 4, 2023 · 8 comments · Fixed by #438
Closed

Doesn't seem to support Jest 28 and newest #429

buugaaga opened this issue Apr 4, 2023 · 8 comments · Fixed by #438

Comments

@buugaaga
Copy link

buugaaga commented Apr 4, 2023

I have tried upgrading Jest from 27 to 28 and it seems that jest-styled-components doesn't do anything after the upgrade.

I have code similar to your examples

import React from 'react';
import { render } from '@testing-library/react';
import 'jest-styled-components';
import styled from 'styled-components';

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

describe('Test', () => {
  it('works', () => {
    const { container } = render(<Button />);
    expect(container.firstChild).toMatchSnapshot();
  });
});

But the snapshot still have the dynamic classname from styled-components and doesn't include the actual styles:

exports[`Test works 1`] = `
<button
  class="sc-bczRLJ bqIyce"
/>
@mmakarin
Copy link

Same issue here

@Ankit-Garg21
Copy link

Getting the same issue with jest 29.x.

@mariczne
Copy link

In our case (although we use it project-wide) the solution was to:

  1. remove import 'jest-styled-components';
  2. create a file that would re-export styleSheetSerializer as default, which is necessary for it to be used in the next step:
// config/jest/styledComponentsSerializer.js
const serializer = require('jest-styled-components').styleSheetSerializer

module.exports = serializer
  1. add the file to snapshotSerializers in jest config, next to enzyme-to-json/serializer, for example:
    "snapshotSerializers": [
        "enzyme-to-json/serializer",
        "<rootDir>/config/jest/styledComponentsSerializer",
    ],

@tim-soft
Copy link

tim-soft commented Oct 9, 2023

@probablyup we've noticed that snapshotting with styled-components v6 + jest-styled-components is entirely broken, do you have any suggestions? This makes testing styled-components rather tricky

@quantizor
Copy link
Contributor

Could be due to something with the new version of stylis

@coravacav
Copy link

Unsure if it's related, but after trying to upgrade to SC6, both this breaks as well as styles in general (I can't use toHaveStyle or similar)

@quantizor
Copy link
Contributor

Thanks for the callout, I found one bug that I patched and shipped as styled-components@6.0.9 that I believe fixes a number of issues around test environments involving JSDOM.

There is a second issue with v6 where stylis is not rendering a space character where it used to... seeing if I can handle that in the library here so people don't need to adjust their assertions.

@quantizor
Copy link
Contributor

@buugaaga it's hard to tell from your bug report but if you're on styled-components v6 I think the 6.0.9 upgrade will fix that bug you're seeing and the jest version might potentially be a red herring

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

Successfully merging a pull request may close this issue.

7 participants