-
Notifications
You must be signed in to change notification settings - Fork 408
toHaveStyle not picking up styles from styled-components #295
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
Comments
For style related issues we need the full markup. It's likely that this is caused by |
Same here. Tried upgrading project dependencies today and noticed all tests that use |
@eps1lon, in an actual browser it works just fine, the problem is with the tests. I downgraded to thanks, both of you! |
Why was this issue closed? |
@felixmagnus in the jsdom environment where tests usually run the components are mounted in isolation, and stylesheets are not normally loaded/mounted in the DOM. I wrote more about it in #113 (comment). Check for instance what we have to do in the very tests of this library so that a bunch of css is loaded, so it can be recognized by jest-dom/src/__tests__/to-have-style.js Lines 13 to 25 in 3fb1835
In other words, this library cannot guarantee that some css is injected into the DOM. Any suggestions are welcome as to how to make it easier. |
@felixmagnus styled-component v5.2.0 makes this error. |
Same issue here, it's a shame because it works if I'm not overriding styles from another component. As soon as I override something, it does not pick up the overridden styles. On the left is my unit test and the right is what |
Same here. I changed the background color of a button via props and it wouldnt pick up the css. |
Can you make sure all needed stylesheets are loaded and properly attached to the DOM in your tests? This is dependent on how your app does CSS. Unfortunately, when we introduced Please consider more end-to-end tests environments for these purposes instead. Test environments that truly work against the fully running app instead of against components mounted in isolation. |
Since we are talking about styled components here, i am not sure if we are able to get that stylesheet as of now: styled-components/styled-components#1018 |
Only thing to test here is the NotificationDot color. Unfortunately, the toHaveStyle assertion is temperamental and does not work for our case testing-library/jest-dom#295 (comment) CP-3824
Hello, this issue happened to me as well. I noticed that the styled component version was "styled-components": "^6.0.0-rc.1", which seems to cause the error, what I did I reverted back to version "styled-components": "5.3.3", and now the tests run fine. I hope this helps you! |
@testing-library/jest-dom
version: 5.11.4react-testing-library
version: 10.4.9 (checked in yarn.lock)node
version: 13.7.0npm
(oryarn
) version: 1.22.4 (yarn)Relevant code or config:
What you did:
Trying to test the final styles for a component using "toHaveStyle" matcher.
This test worked fine before I
yarn upgrade
d the project.What happened:
After upgrading my dependencies, some tests stopped working (the ones including toHaveStyle matcher).
Other tests (.toBeInTheDocument, .toHaveClassName...) still work fine.
It appears to be picking the component base styles (not considering styles applied by classnames / styled-components),
The error log looks like this:
I never apply a "ButtonFace" style, this makes me think it's jest that is not picking up the stylesheets or the styles for some reason.
also relevant:
The Button component works just fine, tested imported in other projects and with playroom-js.
It's just the tests that are not getting the styles properly (and previously did).
Reproduction:
Unfortunately, this happens in a private repo (not the owner), so it's kind of complicated to provide a full demo.
If you need any more information (tests setup, package.json, etc) I can gladly provide it.
Problem description:
Tests were working fine previous to running "yarn upgrade", which updated the testing-related libraries to newer versions
Suggested solution:
I'm not sure how could this be solved, I'm guessing the newer versions of the libraries introduced some breaking changes or changes in configuration which I am not addressing
The text was updated successfully, but these errors were encountered: