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

Memory Leak When Using waitFor

Open
#881 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

  • @testing-library/dom version: 7.29.4
  • Testing Framework and version: N/A
  • DOM Environment: 16.4.0

I've spent the last few days trying to debug what is going on here but I'm afraid I've reached the limit of my knowledge of Node and JS. It seems that there is some sort of memory leak related to the usage of MutationObserver in waitFor.

I've created a bare bones reproduction of the issue in this repo.

The hope is that each test in this bare bones reproduction could be entirely standalone from the others. The basic structure behind the test is as follows:

  1. Create a JSDOM instance
  2. Load a React app bundle in the JSDOM instance
  3. Use @testing-library/dom to assert against that DOM

In the repro you can see that we have the same test repeated 5 times. The log output from running npm test indicates about ~15mb of leaked memory in each test. I suspect this value is the actual JSDOM instance but I am unable to determine what is holding onto the reference.

image

The reason I think it may be related to MutationObserver is that if I comment this line (and any references to observer) then the leaking stops.

I'm really at a loss here so hoping somebody with more familiarity with this sort of thing can shine some light on the issue. Thanks!

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 bare-bones reproduction repository and run its npm test command to confirm the per-test memory growth. Then inspect src/wait-for.js around line 102 and the related observer references; done means the repeated standalone JSDOM tests no longer retain roughly 15 MB per test while waitFor assertions still work.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.