testing-library / testing-library/dom-testing-library

`screen` incompatible with document.body.replaceWith()

Open
#1,310 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
3.3k
Forks
474
PR merge metrics
No merged PRs in 30d

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the screen implementation and the existing regression location in src/tests/screen.js. Reproduce the document.body.replaceWith() case, then verify that screen.getByText, screen.findByText, and screen.queryByText resolve against the replacement body. Done means the shown test passes without using direct document.body queries.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.