Skip to content

Commit c5fcd72

Browse files
committed
feat: add advanceTimers option
1 parent 9fc6a75 commit c5fcd72

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

src/config.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
626
let configForRTL = {
727
reactStrictMode: false,
28+
advanceTimers: maybeAdvanceJestTimers,
829
}
930

1031
function 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

src/pure.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@ import act, {
1313
import {fireEvent} from './fire-event'
1414
import {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-
3016
configureDTL({
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

types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ export * from '@testing-library/dom'
1515

1616
export 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

2026
export interface ConfigFn {

0 commit comments

Comments
 (0)