In react testing library, we have two functions called toBeInTheDocument()
and toBeVisible()
.
1 expect(screen.getByText('hello')).toBeInTheDocument();
2 expect(screen.getByText('hello')).toBeVisible();
It seems both assertions behave the same. What is their difference, and what are the use cases for each?
According to the testing-library/jest-dom documentation,
toBeInTheDocument simply finds element is in DOM Tree regardless of visibility
toBeVisible checks for multiple attributes to see if it's visible such as
display
not equal to none
opacity
greater than 0
hidden
attribute does not existvisibility
not equal to hidden
or collapse
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With