-
Notifications
You must be signed in to change notification settings - Fork 399
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
toBeVisible() returning true but it should return false #116
Comments
It would be helpful if you provide a reproducible example, maybe in code sandbox or a repo that can be cloned and tried live in a browser. Or at the very least help us dig into the details, since you have the example live in your codebase. One reason I can think of is that this library hides the elements via css classes, but if these classes are not loaded in the jsdom |
@gnapse I'll see if I can cook up an example, it does use CSS classes like you mentioned. Is there a way around this? |
If it uses classes, then indeed, this will not work unless that stylesheet is attached to the As you can see, it's not straightforward to do, and also, you need to have access to the entire stylesheet codebase. I guess there could be ways to also manually attach a stylesheet file instead, by dynamically adding a This has come up so many times, and even once someone proposed adding a helper to attach stylesheets to the dom in the context of the text. For starters, in your case, do you have access to that stylesheet, or is something that this library generates dynamically under the hood? Another alternative is to test for the presence or not of certain class names using Anyway, I hope I gave you some pointers, but that's the best I can do. Let me know fi any of this helps, or you with all this in mind you have a suggestion on how jest-dom as a library could help others deal with these situations. This is something we may have to think how to solve or more easily deal with. |
@gnapse Thank you for the reply! It sorta makes sense now. The struggle is we're using styled-components which doesn't expose a stylesheet afaik 😓 I guess I'll try and find another way to do this. Thanks again, |
If the styled components use generates in the test environment a somewhat predictable class name, you could check it with expect(element).toHaveClass(expect.stringContaining('hidden')) This would pass if the element has any class name that contains the substring "hidden". To be clear, the above is a hypothetical that I'm proposing to support. It does not yet work that way. |
That would help immensely if Styled-components were kind enough to do that by default 😓 More often than not we don't have access to any of the styling of an external package unfortunately |
Hey im going throught the same issue here, i want to test a component that should not be visibile bc it has a class called is-hidden that adds the property |
Indeed. Not ideal. But as it's explained before, CSS stylesheets are not mounted in the headless jsdom browser environment, where you mount components in isolation. Unlike when running the entire app. You'd either need to make sure that the stylesheets are loaded (something that depends a lot on how you do your CSS), or you test these things via a more e2e-like environment (Cypress, playwright, etc.) |
I get the limitations with jsdom here, but, man, this feels against the ethos of Testing Library. Having to check for a class instead of "visibility" is a test that relies on how the dev implemented visibility and not "focus on tests that closely resemble how your web pages are interacted by the users". Unfortunate. |
I fully agree. Do you have suggestions about how to better deal with this? I'd love for it to be better, but I do not know what to do. Stylesheets are not mounted in jsdom when mounting components in isolation, as that's something that usually the app's build systems do when you build the actual assets that will get served in a real browser. |
It's obvious now, thanks for the explanation, @gnapse! I have no suggestions on how to better deal with this, but maybe a small warning on the docs will help to avoid these confusions, I can send a proposal via PR if you like. Edit: I've already seen your PR #428 which clarifies the visibility related concepts. Thanks! |
Just want to share that this library worked for me https://github.com/dferber90/jest-transform-css |
jest-dom
version: 3.5.0node
version:v11.9.0npm
version:6.7.0We have an accordion that renders like so:
As you can see it defaults to being closed.
The elements inside should thus be hidden, but
Errors and says an element inside is visible.
The accordion library we're using is https://github.com/springload/react-accessible-accordion version 2.4.5
The text was updated successfully, but these errors were encountered: