Skip to content

Commit

Permalink
feat(config): Add unstable_advanceTimersWrapper (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Sep 11, 2021
1 parent c7ba193 commit 45830f5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/config.ts
Expand Up @@ -12,14 +12,15 @@ interface InternalConfig extends Config {
let config: InternalConfig = {
testIdAttribute: 'data-testid',
asyncUtilTimeout: 1000,
// this is to support React's async `act` function.
// asyncWrapper and advanceTimersWrapper is to support React's async `act` function.
// forcing react-testing-library to wrap all async functions would've been
// a total nightmare (consider wrapping every findBy* query and then also
// updating `within` so those would be wrapped too. Total nightmare).
// so we have this config option that's really only intended for
// react-testing-library to use. For that reason, this feature will remain
// undocumented.
asyncWrapper: cb => cb(),
unstable_advanceTimersWrapper: cb => cb(),
eventWrapper: cb => cb(),
// default value for the `hidden` option in `ByRole` queries
defaultHidden: false,
Expand Down
13 changes: 9 additions & 4 deletions src/wait-for.js
Expand Up @@ -51,6 +51,7 @@ function waitFor(

const usingJestFakeTimers = jestFakeTimersAreEnabled()
if (usingJestFakeTimers) {
const {unstable_advanceTimersWrapper: advanceTimersWrapper} = getConfig()
checkCallback()
// this is a dangerous rule to disable because it could lead to an
// infinite loop. However, eslint isn't smart enough to know that we're
Expand All @@ -71,7 +72,9 @@ function waitFor(
// third party code that's setting up recursive timers so rapidly that
// the user's timer's don't get a chance to resolve. So we'll advance
// by an interval instead. (We have a test for this case).
jest.advanceTimersByTime(interval)
advanceTimersWrapper(() => {
jest.advanceTimersByTime(interval)
})

// It's really important that checkCallback is run *before* we flush
// in-flight promises. To be honest, I'm not sure why, and I can't quite
Expand All @@ -84,9 +87,11 @@ function waitFor(
// of parallelization so we're fine.
// https://stackoverflow.com/a/59243586/971592
// eslint-disable-next-line no-await-in-loop
await new Promise(r => {
setTimeout(r, 0)
jest.advanceTimersByTime(0)
await advanceTimersWrapper(async () => {
await new Promise(r => {
setTimeout(r, 0)
jest.advanceTimersByTime(0)
})
})
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions types/config.d.ts
@@ -1,5 +1,10 @@
export interface Config {
testIdAttribute: string
/**
* WARNING: `unstable` prefix means this API may change in patch and minor releases.
* @param cb
*/
unstable_advanceTimersWrapper(cb: (...args: unknown[]) => unknown): unknown
// eslint-disable-next-line @typescript-eslint/no-explicit-any
asyncWrapper(cb: (...args: any[]) => any): Promise<any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit 45830f5

Please sign in to comment.