Skip to content

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

Open
@quisido

Description

@quisido

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.

Activity

linked a pull request that will close this issue on May 20, 2024
MatanBobi

MatanBobi commented on May 22, 2024

@MatanBobi
Member

@quisido Can you please add a reproduction for this? As long as a document exists, JSDOM implicitly creates document.body for you.

quisido

quisido commented on May 22, 2024

@quisido
Author

@quisido Can you please add a reproduction for this? As long as a document exists, JSDOM implicitly creates document.body for you.

Are you unable to reproduce it? I've hit this in two separate projects just by setting container to document.

As far as I'm aware, using container: document in any existing test is the only step required to reproduce this.

MatanBobi

MatanBobi commented on May 22, 2024

@MatanBobi
Member

Two points here:

  1. We do not have the capacity to try and reproduce every issue that's raised in this repo, that's why we ask people who create issues to provide a cloneable reproduction.
  2. When testing layout components was raised here, I created a solution and It doesn't reproduce there: https://stackblitz.com/edit/rtl-template-68mwxh?file=src%2FApp.test.tsx
m-zuro

m-zuro commented on Oct 10, 2024

@m-zuro

@MatanBobi Running into same issue as described here. Here is reproducible example (clone from your StackBlitz but switched from vitest to jest): https://stackblitz.com/edit/stackblitz-starters-3sze3m?file=app%2Flayout.spec.tsx

MatanBobi

MatanBobi commented on Oct 10, 2024

@MatanBobi
Member

@m-zuro Thanks.
In the reproduction you've attached here, the test isn't passing:

● Test Suite › Test

Expected value   undefined
Received:
  undefined

Message:
  Unable to find an element with the text: Test. 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.

Ignored nodes: comments, script, style
<body />

So I think there's a misconfiguration there.

m-zuro

m-zuro commented on Oct 10, 2024

@m-zuro

@MatanBobi sorry, copy/paste job. Updated now, can you re try pls?

● Test Suite › Test

Expected value   undefined

  Received:
    undefined
  
  Message:
    null is not an object (evaluating 'document.body.removeChild')
MatanBobi

MatanBobi commented on Dec 7, 2024

@MatanBobi
Member

Sorry it took me a while @m-zuro. This error is odd to me, the only place we're calling document.body.removeChild is after this if statement:

container.parentNode === document.body

When I run this test locally in our repo, it passes. I'm trying to understand if there's anything different here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @quisido@MatanBobi@m-zuro

      Issue actions

        When `container` is `document`, cleanup steps fails with "TypeError: Cannot read properties of null (reading 'removeChild')" · Issue #1329 · testing-library/react-testing-library