Skip to content

Commit 2a97fe6

Browse files
committed
fix: correctly detect global testing framework
1 parent 27681db commit 2a97fe6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/__tests__/setup.test.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { vi } from "vitest";
2-
import { mockAllIsIntersecting } from "../test-utils";
2+
import { mockAllIsIntersecting, setupIntersectionMocking } from "../test-utils";
33

44
vi.hoisted(() => {
55
// Clear the `vi` from global, so we can detect if this is a test env
66
// @ts-ignore
77
window.vi = undefined;
88
});
99

10+
afterEach(() => {
11+
vi.resetAllMocks();
12+
});
13+
1014
test("should warn if not running in test env", () => {
1115
vi.spyOn(console, "error").mockImplementation(() => {});
1216
mockAllIsIntersecting(true);
@@ -26,3 +30,10 @@ afterEach(() => {
2630
resetIntersectionMocking();
2731
});`);
2832
});
33+
34+
test('should not warn if running in test env with global "vi"', () => {
35+
vi.spyOn(console, "error").mockImplementation(() => {});
36+
setupIntersectionMocking(vi.fn);
37+
mockAllIsIntersecting(true);
38+
expect(console.error).not.toHaveBeenCalled();
39+
});

src/test-utils.ts

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ function isMocking() {
3434
if (util && typeof util.isMockFunction === "function") {
3535
return util.isMockFunction(window.IntersectionObserver);
3636
}
37+
38+
// No global test utility found. Check if the IntersectionObserver was manually mocked.
39+
if (
40+
typeof window !== "undefined" &&
41+
window.IntersectionObserver &&
42+
"mockClear" in window.IntersectionObserver
43+
) {
44+
return true;
45+
}
46+
3747
return false;
3848
}
3949

0 commit comments

Comments
 (0)