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 utils broken due to observer map

See original GitHub issue

Describe the bug

There’s a cache of observers here: https://github.com/thebuilder/react-intersection-observer/blob/ef0bb84363601992d55a48e26ee19faeb3dd0303/src/observe.ts#L3

This prevents new IntersectionObservers from being created.

test-utils maintains a map of IntersectionObserver to observed Item: https://github.com/thebuilder/react-intersection-observer/blob/d3608a6b6ed21d6e39daaf65005d99514702ec3f/src/test-utils.ts#L50

Entries are inserted when new IntersectionObservers are constructed. This map is used to iterate over all observers when using the mock functions. However, the map is cleared after every test. Due to the observer cache mentioned above, subsequent tests will reuse an observer which is no longer in the test-utils observer map. This breaks all the mock functions if the same useInView is tested in multiple it blocks.

To Reproduce Try and recreate the issue in a Codesandbox:

Hopefully the description above is clear enough.

Expected behavior Mock functions should work across multiple tests

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
thebuildercommented, Nov 19, 2021

Good catch. Should be simple enough to fix - The afterEach needs to call unobserve on each observer before clearing it.

0reactions
thebuildercommented, Nov 25, 2021

@yepitschunked I’ve reverted the change in #523 - test-utils was never suppose to import any of the files, but because it’s compiled with TypeScript it has no knowledge of the bundles.

I’ll need to figure out a better way if this is an actual issue. Can you write out the basic steps you take to cause the issue, so I can replicate it in a test - I haven’t actually been able to do it myself.

Reading your description again, I think the issue might be elsewhere - Are the components unmounted after each it? https://testing-library.com/docs/react-testing-library/api/#cleanup

Read more comments on GitHub >

github_iconTop Results From Across the Web

Some tests break even when mocking IntersectionObserver
Hello, I'm having a bit of an issue with testing, using react-intersection-observer. I'm using it in a component that's being used by many ......
Read more >
How to mock ResizeObserver to work in unit tests using react ...
My problem is that when I go to run my units test it breaks all my tests and looking at the snapshot it...
Read more >
Java Unit Testing with JUnit and TestNG
Regression Test: Tests to ensure the a change (such as enhancement, patches or configuration change) does not break the system or introduce new...
Read more >
Help! My Vue.js Tests are Breaking! - Highland Solutions
I updated Vue Test Utils to 1.0.0-beta.30 and refactored all the broken tests in our project with no major issues using the patterns...
Read more >
Mobile layout broken in Experience Builder - Esri Community
When opening an application that used to work just fine on my mobile device, it now displays the error: undefined is not a...
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