testing-library / testing-library/dom-testing-library
Memory Leak When Using waitFor
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 3.3k
- Forks
- 474
- PR merge metrics
- No merged PRs in 30d
Description
@testing-library/domversion: 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:
- Create a JSDOM instance
- Load a React app bundle in the JSDOM instance
- 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.

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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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