The component that I have testing renders something this:
<div>Text<span>span text</span></div>
As it turns out for testing the only reliable text that I have is the 'span text' but I want to get the 'Text' part of the <div>
. Using Jest and react-testing-library I can
await screen.findByText(spanText)
This returns an HTMLElement but it seems limited as I don't have any of the context around the element. For example HTML methods like parentNode and previousSibling return null or undefined. Ideally I would like to get the text content of the parent <div>
. Any idea how I can do this with either Jest or react-testing-library?
getByText()
returns a DOMElement
. Soclosest()
is a standard js function (and not part of the testing library): developer.mozilla.org/en-US/docs/Web/API/Element/closest – Hensel