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 {
33 configure as configureDTL ,
44} from '@testing-library/dom'
55
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+
626let configForRTL = {
727 reactStrictMode : false ,
28+ advanceTimers : maybeAdvanceJestTimers ,
829}
930
1031function getConfig ( ) {
@@ -21,13 +42,14 @@ function configure(newConfig) {
2142 newConfig = newConfig ( getConfig ( ) )
2243 }
2344
24- const { reactStrictMode, ...configForDTL } = newConfig
45+ const { reactStrictMode, advanceTimers , ...configForDTL } = newConfig
2546
2647 configureDTL ( configForDTL )
2748
2849 configForRTL = {
2950 ...configForRTL ,
3051 reactStrictMode,
52+ advanceTimers,
3153 }
3254}
3355
Original file line number Diff line number Diff line change @@ -13,20 +13,6 @@ import act, {
1313import { fireEvent } from './fire-event'
1414import { getConfig , configure } from './config'
1515
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-
3016configureDTL ( {
3117 unstable_advanceTimersWrapper : cb => {
3218 return act ( cb )
@@ -42,14 +28,12 @@ configureDTL({
4228 // Drain microtask queue.
4329 // Otherwise we'll restore the previous act() environment, before we resolve the `waitFor` call.
4430 // 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 => {
4632 setTimeout ( ( ) => {
4733 resolve ( )
4834 } , 0 )
4935
50- if ( jestFakeTimersAreEnabled ( ) ) {
51- jest . advanceTimersByTime ( 0 )
52- }
36+ await getConfig ( ) . advanceTimers ( 0 )
5337 } )
5438
5539 return result
Original file line number Diff line number Diff line change @@ -15,6 +15,12 @@ export * from '@testing-library/dom'
1515
1616export interface Config extends ConfigDTL {
1717 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 )
1824}
1925
2026export interface ConfigFn {
You can’t perform that action at this time.
0 commit comments