File tree 2 files changed +22
-1
lines changed
2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 1
1
import { vi } from "vitest" ;
2
- import { mockAllIsIntersecting } from "../test-utils" ;
2
+ import { mockAllIsIntersecting , setupIntersectionMocking } from "../test-utils" ;
3
3
4
4
vi . hoisted ( ( ) => {
5
5
// Clear the `vi` from global, so we can detect if this is a test env
6
6
// @ts -ignore
7
7
window . vi = undefined ;
8
8
} ) ;
9
9
10
+ afterEach ( ( ) => {
11
+ vi . resetAllMocks ( ) ;
12
+ } ) ;
13
+
10
14
test ( "should warn if not running in test env" , ( ) => {
11
15
vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
12
16
mockAllIsIntersecting ( true ) ;
@@ -26,3 +30,10 @@ afterEach(() => {
26
30
resetIntersectionMocking();
27
31
});` ) ;
28
32
} ) ;
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -34,6 +34,16 @@ function isMocking() {
34
34
if ( util && typeof util . isMockFunction === "function" ) {
35
35
return util . isMockFunction ( window . IntersectionObserver ) ;
36
36
}
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
+
37
47
return false ;
38
48
}
39
49
You can’t perform that action at this time.
0 commit comments