-
Notifications
You must be signed in to change notification settings - Fork 467
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
screen
incompatible with document.body.replaceWith()
#1310
Comments
I'm hesitating whether this is a valid use case we should be supporting. What are the use cases to actually replace the entire body of the document? This isn't something done as part of frameworks and since there's a workaround to this, I'm not sure we should be providing a solution as part of our code. |
I was able to get this to work in #1311. But right now the types are failing and I'm uncertain how to resolve them. I'll copy the rationale here which I feel better explains my use case: My integration testing environment strives to creates testing scenarios
To assist with this, parts of my integration testing environment involve
Loading the HTML document in Jest is a challenge as the global HTML
This works out great in practice for me. The JavaScript is tested |
@testing-library/dom
version:main
Relevant code or config:
What you did:
Used
Element.replaceWith()
in a test environment to help build full realistic HTML DOMs to verify application behavior.https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceWith
What happened:
Received the error:
TestingLibraryElementError: Unable to find an element with the text: replaceWith element. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
Reproduction:
Problem description:
The current behavior is a problem for me as it makes
screen
unusable. The test environment usesreplaceWith
in an attempt to build a full production accurate HTML document, not just a component fragment within an empty document.Looking into this a bit, when
screen
is imported, it caches the currentdocument.body
and uses that for the remainder of the test. When usingreplaceWith
that cached element becomes invalid and is no longer the same as the body in the document.As a workaround, I use
getByText(document.body, ...)
.Suggested solution:
For this use case to work requires
screen
to dynamically use the currentdocument.body
on each access rather than a cached value. Something like a getter or a function rather than just a value.Another thought would be to provide some API to reload and recache the
screen
value.The text was updated successfully, but these errors were encountered: