Skip to content

Commit c720914

Browse files
committed
fix: always recreate the IntersectionObserver mock
1 parent 189133f commit c720914

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/test-utils.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,20 @@ const originalIntersectionObserver =
3434
*/
3535
if (
3636
typeof window !== "undefined" &&
37-
typeof beforeAll !== "undefined" &&
3837
typeof beforeEach !== "undefined" &&
3938
typeof afterEach !== "undefined"
4039
) {
41-
const initMocking = () => {
40+
beforeEach(() => {
4241
// Use the exposed mock function. Currently, it supports Jest (`jest.fn`) and Vitest with globals (`vi.fn`).
4342
// @ts-ignore
4443
if (typeof jest !== "undefined") setupIntersectionMocking(jest.fn);
4544
else if (typeof vi !== "undefined") {
4645
setupIntersectionMocking(vi.fn);
4746
}
48-
};
47+
// Ensure there's no observers from previous tests
48+
observers.clear();
49+
});
4950

50-
beforeAll(initMocking);
51-
beforeEach(initMocking);
5251
afterEach(resetIntersectionMocking);
5352
}
5453

@@ -95,7 +94,6 @@ afterEach(() => {
9594
* @param mockFn The mock function to use. Defaults to `vi.fn`.
9695
*/
9796
export function setupIntersectionMocking(mockFn: typeof vi.fn) {
98-
if (isMocking()) return;
9997
window.IntersectionObserver = mockFn((cb, options = {}) => {
10098
const item = {
10199
callback: cb,

0 commit comments

Comments
 (0)