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

add snapshot options #375

Merged

Conversation

jantimon
Copy link
Contributor

@jantimon jantimon commented May 27, 2021

We are using jest-styled-components in a large codebase and it works perfectly.
However sometimes we would love to have more control over the snapshots generation.

This pr shows how a setStyleSheetSerializerOptions might be added which would allow:

  • generate snapshots with css
  • generate snapshots without css (but still format classNames from hashes to c0)
  • generate snapshots without css but still make use of toHaveStyleRule
  • allows to format the className e.g. styledComponent0 instead of c0

Example usage:

import 'jest-styled-components';
import { setStyleSheetSerializerOptions } from 'jest-styled-components/serializer';

describe('style tests', () => {
  test('it renders correct styles for X', () => { 
    const { container } = render(<Button />) 
    expect(container.firstChild).toMatchSnapshot()})
  })
}) 

describe('other tests', () => {
  beforeEach(() => {
    setStyleSheetSerializerOptions({ addStyles: false })
  });
  test('it renders correct markup for case X', () => { 
    const { container } = render(<Button />) 
    expect(container.firstChild).toMatchSnapshot()})
  })
}) 

I am very open to feedback and would love to know what you think

Copy link
Member

@christiansany christiansany left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool addition and enhancement, thanks :D

@christiansany christiansany merged commit b5488fa into styled-components:main Oct 26, 2021
@vadim-sartakov
Copy link

It would be really helpful to have this kind of control over serialization. Any ideas on when it's going to be released?

@protoEvangelion
Copy link

I need this as well. @probablyup any ideas when this could be released?

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

Successfully merging this pull request may close these issues.

4 participants