diff --git a/src/config.js b/src/config.js index 5be8381d..b27b9fd1 100644 --- a/src/config.js +++ b/src/config.js @@ -3,6 +3,14 @@ // './queries' are query functions. let config = { testIdAttribute: 'data-testid', + // this 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(), } export function configure(newConfig) { diff --git a/src/wait-for-dom-change.js b/src/wait-for-dom-change.js index 9855ea9c..ba9be95f 100644 --- a/src/wait-for-dom-change.js +++ b/src/wait-for-dom-change.js @@ -1,4 +1,5 @@ import {newMutationObserver, getDocument, getSetImmediate} from './helpers' +import {getConfig} from './config' function waitForDomChange({ container = getDocument(), @@ -36,4 +37,8 @@ function waitForDomChange({ }) } -export {waitForDomChange} +function waitForDomChangeWrapper(...args) { + return getConfig().asyncWrapper(() => waitForDomChange(...args)) +} + +export {waitForDomChangeWrapper as waitForDomChange} diff --git a/src/wait-for-element-to-be-removed.js b/src/wait-for-element-to-be-removed.js index 5070490d..5cb82f4c 100644 --- a/src/wait-for-element-to-be-removed.js +++ b/src/wait-for-element-to-be-removed.js @@ -1,4 +1,5 @@ import {getDocument, getSetImmediate, newMutationObserver} from './helpers' +import {getConfig} from './config' function waitForElementToBeRemoved( callback, @@ -69,4 +70,8 @@ function waitForElementToBeRemoved( }) } -export {waitForElementToBeRemoved} +function waitForElementToBeRemovedWrapper(...args) { + return getConfig().asyncWrapper(() => waitForElementToBeRemoved(...args)) +} + +export {waitForElementToBeRemovedWrapper as waitForElementToBeRemoved} diff --git a/src/wait-for-element.js b/src/wait-for-element.js index a804aea4..6bcb9dde 100644 --- a/src/wait-for-element.js +++ b/src/wait-for-element.js @@ -1,4 +1,5 @@ import {newMutationObserver, getDocument, getSetImmediate} from './helpers' +import {getConfig} from './config' function waitForElement( callback, @@ -54,4 +55,8 @@ function waitForElement( }) } -export {waitForElement} +function waitForElementWrapper(...args) { + return getConfig().asyncWrapper(() => waitForElement(...args)) +} + +export {waitForElementWrapper as waitForElement} diff --git a/src/wait.js b/src/wait.js index 46205cd7..379c3f52 100644 --- a/src/wait.js +++ b/src/wait.js @@ -1,7 +1,12 @@ import waitForExpect from 'wait-for-expect' +import {getConfig} from './config' function wait(callback = () => {}, {timeout = 4500, interval = 50} = {}) { return waitForExpect(callback, timeout, interval) } -export {wait} +function waitWrapper(...args) { + return getConfig().asyncWrapper(() => wait(...args)) +} + +export {waitWrapper as wait} diff --git a/typings/config.d.ts b/typings/config.d.ts index 6bb7c538..1993dfab 100644 --- a/typings/config.d.ts +++ b/typings/config.d.ts @@ -1,5 +1,6 @@ export interface IConfig { testIdAttribute: string + asyncWrapper(cb: Function): Promise } export interface IConfigFn {