testing-library / testing-library/react-testing-library

When `container` is `document`, cleanup steps fails with "TypeError: Cannot read properties of null (reading 'removeChild')"

Open
#1,329 7 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
19.7k
Forks
1.2k
Avg merge
3d 16h
Merged PRs (30d)
1

Description

Just opening this for tracking with the associated PR: #1330 .

When:

render(<Component />, { container: document });

(Untested: This bug may require that Component render a <body> element.)

Then:

TypeError: Cannot read properties of null (reading 'removeChild')

Because:

// pure.js
    if (container.parentNode === document.body) {
      document.body.removeChild(container);
    }

If we first check that document.body is not null, then this error will not occur.

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 in pure.js at the cleanup condition that calls document.body.removeChild(container). Reproduce render(, { container: document }) and verify cleanup when document.body is null; done means the TypeError no longer occurs. The issue mentions associated PR #1330, so check its status before starting.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
testing-qa
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.