File tree Expand file tree Collapse file tree 3 files changed +31
-19
lines changed Expand file tree Collapse file tree 3 files changed +31
-19
lines changed Original file line number Diff line number Diff line change @@ -3,8 +3,29 @@ import {
3
3
configure as configureDTL ,
4
4
} from '@testing-library/dom'
5
5
6
+ function jestFakeTimersAreEnabled ( ) {
7
+ /* istanbul ignore else */
8
+ if ( typeof jest !== 'undefined' && jest !== null ) {
9
+ return (
10
+ // legacy timers
11
+ setTimeout . _isMockFunction === true || // modern timers
12
+ // eslint-disable-next-line prefer-object-has-own -- No Object.hasOwn in all target environments we support.
13
+ Object . prototype . hasOwnProperty . call ( setTimeout , 'clock' )
14
+ )
15
+ } // istanbul ignore next
16
+
17
+ return false
18
+ }
19
+
20
+ function maybeAdvanceJestTimers ( delay = 0 ) {
21
+ if ( jestFakeTimersAreEnabled ( ) ) {
22
+ jest . advanceTimersByTime ( delay )
23
+ }
24
+ }
25
+
6
26
let configForRTL = {
7
27
reactStrictMode : false ,
28
+ advanceTimers : maybeAdvanceJestTimers ,
8
29
}
9
30
10
31
function getConfig ( ) {
@@ -21,13 +42,14 @@ function configure(newConfig) {
21
42
newConfig = newConfig ( getConfig ( ) )
22
43
}
23
44
24
- const { reactStrictMode, ...configForDTL } = newConfig
45
+ const { reactStrictMode, advanceTimers , ...configForDTL } = newConfig
25
46
26
47
configureDTL ( configForDTL )
27
48
28
49
configForRTL = {
29
50
...configForRTL ,
30
51
reactStrictMode,
52
+ advanceTimers,
31
53
}
32
54
}
33
55
Original file line number Diff line number Diff line change @@ -13,20 +13,6 @@ import act, {
13
13
import { fireEvent } from './fire-event'
14
14
import { getConfig , configure } from './config'
15
15
16
- function jestFakeTimersAreEnabled ( ) {
17
- /* istanbul ignore else */
18
- if ( typeof jest !== 'undefined' && jest !== null ) {
19
- return (
20
- // legacy timers
21
- setTimeout . _isMockFunction === true || // modern timers
22
- // eslint-disable-next-line prefer-object-has-own -- No Object.hasOwn in all target environments we support.
23
- Object . prototype . hasOwnProperty . call ( setTimeout , 'clock' )
24
- )
25
- } // istanbul ignore next
26
-
27
- return false
28
- }
29
-
30
16
configureDTL ( {
31
17
unstable_advanceTimersWrapper : cb => {
32
18
return act ( cb )
@@ -42,14 +28,12 @@ configureDTL({
42
28
// Drain microtask queue.
43
29
// Otherwise we'll restore the previous act() environment, before we resolve the `waitFor` call.
44
30
// The caller would have no chance to wrap the in-flight Promises in `act()`
45
- await new Promise ( resolve => {
31
+ await new Promise ( async resolve => {
46
32
setTimeout ( ( ) => {
47
33
resolve ( )
48
34
} , 0 )
49
35
50
- if ( jestFakeTimersAreEnabled ( ) ) {
51
- jest . advanceTimersByTime ( 0 )
52
- }
36
+ await getConfig ( ) . advanceTimers ( 0 )
53
37
} )
54
38
55
39
return result
Original file line number Diff line number Diff line change @@ -15,6 +15,12 @@ export * from '@testing-library/dom'
15
15
16
16
export interface Config extends ConfigDTL {
17
17
reactStrictMode : boolean
18
+ /**
19
+ * A function to be called internally to advance your fake timers (if applicable)
20
+ *
21
+ * @example jest.advanceTimersByTime
22
+ */
23
+ advanceTimers ?: ( ( delay : number ) => Promise < void > ) | ( ( delay : number ) => void )
18
24
}
19
25
20
26
export interface ConfigFn {
You can’t perform that action at this time.
0 commit comments