@@ -4,11 +4,40 @@ import * as crypto from 'node:crypto';
4
4
import { TextDecoder , TextEncoder } from 'node:util' ;
5
5
6
6
import { cleanup , configure } from '@testing-library/react' ;
7
- import { afterAll , afterEach , beforeAll , vi } from 'vitest' ;
7
+ import { afterAll , afterEach , beforeAll , beforeEach , vi } from 'vitest' ;
8
8
9
9
configure ( { } ) ;
10
10
11
- afterEach ( cleanup ) ;
11
+ // Track all timers created during tests to clean them up
12
+ const activeTimers = new Set < ReturnType < typeof setTimeout > > ( ) ;
13
+ const originalSetTimeout = global . setTimeout ;
14
+ const originalClearTimeout = global . clearTimeout ;
15
+
16
+ // Wrap setTimeout to track all timers
17
+ global . setTimeout = ( ( callback : any , delay ?: any , ...args : any [ ] ) => {
18
+ const timerId = originalSetTimeout ( callback , delay , ...args ) ;
19
+ activeTimers . add ( timerId ) ;
20
+ return timerId ;
21
+ } ) as typeof setTimeout ;
22
+
23
+ // Wrap clearTimeout to remove from tracking
24
+ global . clearTimeout = ( ( timerId ?: ReturnType < typeof setTimeout > ) => {
25
+ if ( timerId ) {
26
+ activeTimers . delete ( timerId ) ;
27
+ originalClearTimeout ( timerId ) ;
28
+ }
29
+ } ) as typeof clearTimeout ;
30
+
31
+ beforeEach ( ( ) => {
32
+ activeTimers . clear ( ) ;
33
+ } ) ;
34
+
35
+ afterEach ( ( ) => {
36
+ cleanup ( ) ;
37
+ // Clear all tracked timers to prevent post-test execution
38
+ activeTimers . forEach ( timerId => originalClearTimeout ( timerId ) ) ;
39
+ activeTimers . clear ( ) ;
40
+ } ) ;
12
41
13
42
// Store the original method
14
43
// eslint-disable-next-line @typescript-eslint/unbound-method
@@ -43,6 +72,7 @@ if (typeof window !== 'undefined') {
43
72
TextEncoder : { value : TextEncoder } ,
44
73
Response : { value : FakeResponse } ,
45
74
crypto : { value : crypto . webcrypto } ,
75
+ isSecureContext : { value : true , writable : true } ,
46
76
} ) ;
47
77
48
78
// Mock ResizeObserver
0 commit comments