Description
@testing-library/dom
version:main
- Testing Framework and version: Jest 29.7.0
- DOM Environment: jsdom 20.0.0
Relevant code or config:
// src/__tests__/screen.js
test('queries after replaceWith', async () => {
const newBody = document.createElement('body')
newBody.innerHTML = '<div>replaceWith element</div>'
document.body.replaceWith(newBody)
screen.getByText('replaceWith element')
await screen.findByText('replaceWith element')
expect(screen.queryByText('replaceWith element')).not.toBeNull()
})
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:
// src/__tests__/screen.js
test('queries after replaceWith', async () => {
const newBody = document.createElement('body')
newBody.innerHTML = '<div>replaceWith element</div>'
document.body.replaceWith(newBody)
screen.getByText('replaceWith element')
await screen.findByText('replaceWith element')
expect(screen.queryByText('replaceWith element')).not.toBeNull()
})
Problem description:
The current behavior is a problem for me as it makes screen
unusable. The test environment uses replaceWith
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 current document.body
and uses that for the remainder of the test. When using replaceWith
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 current document.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.