Skip to content

Commit

Permalink
fix: always recreate the IntersectionObserver mock
Browse files Browse the repository at this point in the history
  • Loading branch information
thebuilder committed Jan 15, 2025
1 parent 189133f commit c720914
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,20 @@ const originalIntersectionObserver =
*/
if (
typeof window !== "undefined" &&
typeof beforeAll !== "undefined" &&
typeof beforeEach !== "undefined" &&
typeof afterEach !== "undefined"
) {
const initMocking = () => {
beforeEach(() => {
// Use the exposed mock function. Currently, it supports Jest (`jest.fn`) and Vitest with globals (`vi.fn`).
// @ts-ignore
if (typeof jest !== "undefined") setupIntersectionMocking(jest.fn);
else if (typeof vi !== "undefined") {
setupIntersectionMocking(vi.fn);
}
};
// Ensure there's no observers from previous tests
observers.clear();
});

beforeAll(initMocking);
beforeEach(initMocking);
afterEach(resetIntersectionMocking);
}

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

0 comments on commit c720914

Please sign in to comment.