question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Test really slow because of MutationObserver not disconnecting directly

See original GitHub issue
  • @testing-library/dom version: 7.26.3
  • Testing Framework and version: @testing-library/react version: 11.1.0 jest version: 26.6.1
  • DOM Environment: jsdom version: 16.4.0

Hello guys 😃

What happened:

My tests are really slow.

Reproduction:

I made a little codesandbox: https://codesandbox.io/s/mutationobserver-reproduction-pe9gf?file=/src/__tests__/index.js where you can see my problem.

The issue is that the callback of waitFor is called even when the element has been found: image

The console log is printed thrice instead of one: image

Problem description:

The MutationObserver is disconnected at the end of the test instead of directly when the onDone callback is called. I guess this is because the thread is not available until the end of the test. These unnecessary callback’s calls (in the MutationObserver) make my tests really slow as you can see below:

image

Suggested solution:

I have tested to directly disconnect the observer instead of inside the setImmediate method (https://github.com/testing-library/dom-testing-library/blob/master/src/wait-for.js#L97). I get really faster tests:

if (!usingFakeTimers) {
    clearInterval(intervalId)
    observer.disconnect()
}

image

Is there a reason to disconnect the observer inside setImmediate instead of synchronously ? Am I doing something wrong ? Or maybe I could make a PR to make the disconnection configurable globally (choice would be between synchronously and asynchronously) ?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, Nov 2, 2020

I don’t think it’s worth the complexity. People should run their code in an environment that has proper support for MutationObserver.

0reactions
romain-trotardcommented, Nov 4, 2020

Again thank you all I close the issue 😃 Pr: #801

Read more comments on GitHub >

github_iconTop Results From Across the Web

MutationObserver.disconnect() - Web APIs - MDN Web Docs
The MutationObserver method disconnect() tells the observer to stop watching for mutations.
Read more >
Performance of MutationObserver to detect nodes in entire DOM
This answer primarily applies to big and complex pages. If attached before page load/render, an unoptimized MutationObserver callback can ...
Read more >
Solved: How to adapt to the undocumented JavaScript loadin...
Solved: In the most recent Canvas Release Notes (2019-06-22) there was an undocumented change to what we believe is the loading sequence of....
Read more >
Detect, Undo And Redo DOM Changes With Mutation Observers
I've been wondering why DOM Mutation Observers, which can monitor changes to one or more DOM elements and return a report of what...
Read more >
Guide to custom React Hooks with MutationObserver
As a case study, we'll abstract the MutationObserver API in a custom React Hook, demonstrating how we can build robust, shareable pieces of ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found