testing-library / jest-dom

:owl: Custom jest matchers to test the state of the DOM
https://testing-library.com/docs/ecosystem-jest-dom
MIT License
4.45k stars 401 forks source link

toHaveStyles always report hover style #594

Open thany opened 8 months ago

thany commented 8 months ago

Relevant code or config:

render(<Button kind="primary" />);
const button = screen.getByRole('button');
expect(button).toHaveStyles({ 'background-color': '#5FA00C' });

What you did:

The above code should render a button with background-color: #5FA00C and it absolutely does. In a real browser I can see that it has the correct colour.

What happened:

In the unittest, it errors out, telling me that its background colour is rgb(62, 136, 0) instead. This is incorrect. This colour happens to be the hover color. I am not simulating a hover anywhere. So why is it testing for that?

Reproduction:

Forked Codesandbox Although it no longer shows the tests tab, so I have no clue if this actually reproduces the problem. It appears codesandbox made a whoopsie. Sorry, nothing I can do about it.

Problem description:

I seem to be facing the opposite of issue 59 where it ignores hover styles. It always tests for hover styles now, no matter what (seemingly).

Suggested solution:

Don't do a hover on element to test toHaveStyle for, or something. Just ignore the hover styles.

Myky8 commented 7 months ago

I have the same problem!!

oskar-wilczynski-ynd commented 7 months ago

I have the same issue on "@testing-library/jest-dom": "^5.16.1", one solution I found is using a similar method from jest-styled-components called toHaveStyleRule -- works like a charm.

Unfortunately, I guess it works only for styled-components...

Myky8 commented 7 months ago

I found that is caused by "getComputedStyle" of JSDOM.

https://github.com/jsdom/jsdom/issues/3607

mxgit1090 commented 7 months ago

I found that is caused by "getComputedStyle" of JSDOM.

jsdom/jsdom#3607

I have been waiting for the fix for nearly a half year ...

sangquangnguyen commented 7 months ago

I found that is caused by "getComputedStyle" of JSDOM. jsdom/jsdom#3607

I have been waiting for the fix for nearly a half year ...

You could use patch-package first to patch the fix here, no need to wait for nwsapi and jest to release it

thany commented 7 months ago

You could use patch-package first to patch the fix here, no need to wait for nwsapi and jest to release it

For end-user projects paid for by a commercial customer, in my experience, this will create headaches and tech debt in the future - for yourself if the customer is lucky, but web development companies usually enjoy some amount staff turnover.

If a project maintainer is on top of things all the time, and it's a product of love and whatnot, this is okay advice, and perfectly fine to implement in any downstream project or package. So patch-package is a great solution if the jest-dom people would implement it on their end instead 🙏🏻

Waley-Z commented 5 months ago

This issue seems to be caused by a bug (https://github.com/dperini/nwsapi/issues/115) in nwsapi 2.2.10. It will work if I use 2.2.9. Try:

"overrides": {
    "nwsapi": "2.2.9"
}

update: the bug was still not fixed in nwsapi 2.2.12. (https://github.com/dperini/nwsapi/issues/119)