Jest-styled-components: toHaveStyleRule doesn't work with rules with increased specificity

Created on 28 Aug 2020  路  2Comments  路  Source: styled-components/jest-styled-components

Hi,

I want to test the following component which has a rule with increased specificity:

const StyledBodyCopy = styled(BodyCopy)`
  && {
    color: red;
  }
`;

There's also a test to ensure that the color rule is set on the component:

      const { queryByText } = render(<StyledBodyCopy>text</StyledBodyCopy>);
      expect(queryByText('text')).toBeInTheDocument();
      expect(queryByText('text')).toHaveStyleRule('color', 'red');

However, the last check toHaveStyleRule('color', 'red') fails with the following:

   "Property 'color' not found in style rules"

    Expected
      "color: inherit"
    Received:
      "color: undefined"

But the rule is present in the snapshot:

.c0.c0 {
  color: red;
}

<p
  class="c0"
>
  text
</p>

However, if I remove the ampersands, the check succeeds:

const StyledBodyCopy = styled(BodyCopy)`
  color: red;
`;

Most helpful comment

Could you try with modifier: '&&',

    expect(queryByText('text')).toHaveStyleRule('color', 'red', {
      modifier: '&&',
    });

In my case, it's working.

All 2 comments

Could you try with modifier: '&&',

    expect(queryByText('text')).toHaveStyleRule('color', 'red', {
      modifier: '&&',
    });

In my case, it's working.

Ah, didn't know about the third argument. This does the trick. 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrmartineau picture mrmartineau  路  4Comments

dlebedynskyi picture dlebedynskyi  路  8Comments

tdimnet picture tdimnet  路  6Comments

ghost picture ghost  路  7Comments

basslagter picture basslagter  路  3Comments