`toBeVisible` not working as expected with `visibility` property
See original GitHub issue@testing-library/jest-domversion: v5.1.1@testing-library/reactversion: v9.4.0nodeversion: v12.14.1npm(oryarn) version: v1.21.1
Relevant code or config:
import styled from 'styled-components';
const Container = styled.div`
visibility: hidden;
&:hover {
visibility: visible;
}
> input:checked {
visibility: visible;
}
`;
const { getByTestId } = render(
<Container>
<input data-testid="checkbox" type="checkbox" checked />
</Container>
);
expect(getByTestId('checkbox')).toBeVisible();
What you did:
Tried to test a checkbox that must be visible when it has been checked with toBeVisible.
What happened:
Even though the checkbox is visible toBeVisible throws an error because it detects that the parent has visibility: hidden.
Reproduction:
https://codesandbox.io/s/kind-rain-62otm?fontsize=14&hidenavigation=1&previewwindow=tests&theme=dark
Problem description:
My code has a checkbox that must be visible when the user checks it. The visibility property is perfect for this because it overrides its parents’ values.
So, if the checkbox has visibility: visible property, it will be shown regardless of the parent value.
Suggested solution:
Not sure 😦 will come up with something if this is really an issue.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:20
- Comments:28 (13 by maintainers)
Top Results From Across the Web
Cannot check expect(elm).not.toBeVisible() for semantic-ui ...
An element is visible if all the following conditions are met: it does not have its css property display set to none; it...
Read more >toBeVisible | playwright-expect
Use toBeVisible function when you want to check that an element is visible. example: // could be used with Promise<ElementHandle> await expect(page.
Read more >Testing-library toBeVisible, Jest check if element is visible ...
React Testing Library on GitHub; The problem#. ... matches any received object that does not recursively match the expected properties.
Read more >@testing-library/jest-dom - npm
@testing-library/jest-dom can work with any library or framework that ... toBeVisible() expect(getByText('Visibility Hidden Example')).not.
Read more >Expect - Jest
The TypeScript examples from this page will only work as documented if you explicitly import Jest ... objectContaining(object); expect.not.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Perfect. This is indeed a bug.
We’re currently assuming that
visibilityacts likedisplay. Whiledisplay: nonehides any child even if they do setdisplay: block,visibility: hiddencan be overridden. The difference betweendisplayandvisibilityis thatvisibilityis an inherited CSS property whiledisplayis not.We need to account for that now that JSDOM implements CSS inheritance.
Here’s an absolutely minimal example of how
.toBeVisible()is failing for me, with inline SVG:Also in Code Sandbox.
My best guess is that this is a bug in jsdom itself, because the computed style for
visibilityofmy-elementends up beingvisiblehere: https://github.com/testing-library/jest-dom/blob/main/src/to-be-visible.js#L6