react-intersection-observer: Does not recognise Vitest 2 inside `beforeAll` hook
Describe the bug A clear and concise description of what the bug is.
When trying to upgrade Vitest from 1.6 to 2.0 I’m getting these warnings:
React Intersection Observer was not configured to handle mocking.
Outside Jest and Vitest, you might need to manually configure it by calling setupIntersectionMocking() and resetIntersectionMocking() in your test setup file.
// test-setup.js
import { resetIntersectionMocking, setupIntersectionMocking } from 'react-intersection-observer/test-utils';
beforeEach(() => {
setupIntersectionMocking(vi.fn);
});
afterEach(() => {
resetIntersectionMocking();
});
To Reproduce Try and recreate the issue in a Codesandbox:
none yet
Expected behavior A clear and concise description of what you expected to happen.
Should detect Vitest 2 similar to Vitest 1.6
Additional context Add any other context about the problem here.
Running latest version, 9.10.3
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 23 (21 by maintainers)
@condorheroblog should be fixed by #720 released in v9.16.0
@condorheroblog you are right. I forgot to handle global correctly in the last refactor. It’s tricky, since there isn’t a reliable way to determine if the intersectionObserver is mocked.
Hi @thebuilder.
I also encountered this problem.
I didn’t turn on the global mode. I used the
mockIsIntersectingfunction in the test file, which called thewarnOnMissingSetupfunction. Tracing the call chain, I found that this function caused it.Because I don’t have global mode enabled, this function will return undefined and print a warning message.
This is how I use it.
vitest.config.ts@3.0.7vitest.setup.tsFYI @RobinTail - I’ll drop the
beforeAllin #716 It’s simply not safe to rely on just thebeforeAll, since global options in test runners likemockResetcan reset the observer after a test run, preventing it from working in the following.mockAllIsIntersectinginbeforeAlltrueif they aren’t mocked.f72e455(#690)Dear @thebuilder , here is the VM that reproduces the issue:
https://codesandbox.io/p/devbox/crimson-night-w64sxn
In terminal try to execute
yarn test.(you may probably need to be logged in for that)
I’ll try to prepare a sandbox to reproduce it
#690 The tests using the
test-utilsin the library are all running fine after upgrading.