react-intersection-observer: Version >= 9.12.0 breaks automatic InteractionOberserver setup with jest

We have a test environment where we’ve used mockAllIsIntersecting from react-intersection-observer/test-utils together with Jest This has worked just fine until we updated to version 9.12.0 and beyond after that we’ve started to get type errors when running tests.

After that we got the error:

TypeError: observer.observe is not a function

      at observe (node_modules/react-intersection-observer/src/observe.ts:153:12)
      at node_modules/react-intersection-observer/src/useInView.tsx:66:19
      at commitHookEffectListMount (node_modules/react-dom/cjs/react-dom.development.js:23189:26)

The error could be resolved by manually adding this code to our Jest tests:

beforeEach(() => {
  setupIntersectionMocking(jest.fn);
});

afterEach(() => {
  resetIntersectionMocking();
});

So it seems like version 9.12.0 broke the automatic setup with Jest? 🤔

To Reproduce Try and recreate the issue in a Codesandbox:

Expected behavior If react-intersection-observer should still be possible to run together with Jest without manually using setupIntersectionMocking and resetIntersectionMocking types should be fixed.

I suspect that this error comes from this update mentioned in the release notes for version 9.12.0

“Types cleanup in test-utils, getting rid of @types/jest (replaced with vitest)”

Screenshots Skärmavbild 2024-08-21 kl  12 00 22

**Desktop **

  • OS: Mac OS Sonoma
  • Browser N/A (Jest tests)
  • Version >= 9.12.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Jeez, what kind of pseudo reset is that. Seems like Vitest does the same. Can’t find an obvious way to determine if it has been reset. Maybe I have to skip that check then, and always create a new instance.

I’ve now verified that the error isn’t there with version 9.11 it starts with 9.12. I’ve also verified that the error doesn’t occur if I run just one test.

The error occurs only when I have two or more tests that both contains a call to mockAllIsIntersecting(true);

I didn’t have anything that resets between the test before I’ve added a the fix to circumvent this error (the setupIntersectionMocking/resetIntersectionMocking mentioned above)

@julian-jambit that could explain the issue. This change was made because of the issue in #689 - I’m thinking it might sense to just go back to beforeEach to be safe.