question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong style rule used

See original GitHub issue

I’m trying to test some styles on my component and despite all tries I can’t get the test to pass.

After invetigating a little bit, I found that the way you are getting the component rules is what makes the test to fail.

Current behavior: Check this codesandbox: https://codesandbox.io/s/bold-glitter-h8rkb

When testing a styled component, the jest-emotion matcher validates against the wrong style rule.

Expected behavior: The jest-emotion matcher validates against the correct style rule.

More information: At the Checkbox test, I’m trying to validate that the hover state has the correct border, but as is, the matcher will validate the component style against the &:checked &:hover style rule, because it is the last style rule that has the :hover.

I debugged the matcher code locally and found the following:

Selectors for the component (and for the :hoverstate)

[
  '.css-ih6inj-StyledCheckbox:hover'
  '.css-ih6inj-StyledCheckbox:checked:hover',
]

Declarations

[
  {
    type: 'declaration', // This one is for the first selector `:hover`
    property: 'border-color',
    value: '#7E7E7E',
    position: Position { start: [Object], end: [Object], source: undefined }
  },
  {
    type: 'declaration', // This is for the second selector `:hover:checked`
    property: 'border-color',
    value: '#9E9E9E',
    position: Position { start: [Object], end: [Object], source: undefined }
  }
]

The declaration that was used to validate the component style

 {
  type: 'declaration',
  property: 'border-color',
  value: '#9E9E9E',
  position: Position { start: [Object], end: [Object], source: undefined }
 }

Which is the last declaration for the :hover state despite it is only applied when the component is :checked (and the component being unchecked on the test)

If I change the order of the style declaration on the component from:

&:hover ...

&:checked &:hover ...

to

&:checked &:hover ...

&:hover ...

the test passes but then if I test the style for the :hover when the input is :checkedit will fail because the matcher will compare against the last declared style for hover.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nobreferreiracommented, May 21, 2020

I understand that a totally fine tuned matcher for every cases would be very complicated, but in this case we have a very limited matcher.

It would be nice to have at least a matcher that would recognize a rule with more than a pseudo-element and/or combinators targeting a specific element forgetting all the inheritance.

Anyway I think that this should be properly documented.

0reactions
jcleefwcommented, Aug 17, 2021

While I understand that this has been a problem for you and that it’s quite easy to get yourself into such a problem when using this matcher against pseudo-classes, I’m not sure if we can classify this as a bug.

The problem is that we don’t know in what state a particular element is - in the very same way we don’t know that it is in the :hover state so you have to tell us that it is. This target is not even limited to the root element - it would work for styles such as & span:hover - which makes this even more complicated.

To make this work for your case you could use such target: /^[^\:]+:hover/. This would disallow any other pseudos preceding the hover.

I agree that the target-matching algorithm could be probably improved, but I’m afraid I won’t have time to do it myself. PRs are always welcome!

This solution does work but the typing that toHaveStyleRule for target only receives RegExp. To allow that I’ve created a PR #2457

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS - Cascading Style Sheets, designing for the Web - W3C
A style sheet is a set of one or more rules that apply to an HTML document. The rule above sets ... Any...
Read more >
CSS syntax - Cascading Style Sheets - MDN Web Docs
Any statement which isn't a ruleset or an at-rule is invalid and ignored. Nested statements. There is another group of statements – the...
Read more >
Chapter 4 Flashcards - Quizlet
The style rule used to set the same font family property and values for the header ... the location of each error so...
Read more >
Why does "<! --" comment out a style rule, but "<!--" does not?
The string <! -- does not comment out anything. Instead, in CSS it is a syntax error ...
Read more >
About Cascading Style Sheets - Life Sciences
Inline styles are those that are used as part of the HTML tag itself. ... of style rules. Let's first look at how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found