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

Library is not applied to tests where the Renderer object was created in a 'describe' #428

Open
kolesker opened this issue Mar 2, 2023 · 0 comments

Comments

@kolesker
Copy link

kolesker commented Mar 2, 2023

No style rules found on passed component

When renderer is created in the describe, jest-styled-components library is not applied. The snapshot will output as if the library is not imported, hence no styles, only class names with hashes. Therefore, 'no style rules' are found and test fails.

When moving the renderer creating inside the test (test/it), it works as it should and test passes.

Note: this was working in styled-components@ˆ4 and jest-styled-components@ˆ6, and after the upgrade our existing tests are breaking.

import styled from 'styled-components';

export const Label = styled.div`
  display: block;
`;
Label.displayName = 'Label';


// BEFORE - Not working
import React from 'react';
import renderer from 'react-test-renderer';
import 'jest-styled-components';

import { Label } from './label';

describe('Label', () => {
  const tree = renderer.create(<Label />).toJSON();
  it('should match snapshot', () => {
    expect(tree).toHaveStyleRule('display', 'block');
  });
});

// AFTER - Working
import React from 'react';
import renderer from 'react-test-renderer';
import 'jest-styled-components';

import { Label } from './label';

describe('Label', () => {
  it('should match snapshot', () => {
    const tree = renderer.create(<Label />).toJSON();
    expect(tree).toHaveStyleRule('display', 'block');
  });
});

"styled-components": "^5.3.6",
"jest-styled-components": "^7.1.1",
"babel-plugin-styled-components": "^2.0.7",

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

No branches or pull requests

1 participant