'How to set waitFor options globally in React Testing Library?

I was taking a look to the waitFor documentation and I was wondering if there is any way to configure it globally, for example in the jest.config or in the command used to launch the test suite?

I need to increase the timeout in every test and it is a bit annoying.



Solution 1:[1]

Per the documentation, the configuration option for the timeouts on the various asynchronous utilities is asyncUtilTimeout:

asyncUtilTimeout

The global timeout value in milliseconds used by waitFor utilities. Defaults to 1000ms.

You can configure this using the configure function, e.g. in a setupTests.js file:

import { configure } from "@testing-library/react";

configure({ asyncUtilTimeout: 5000 });

Solution 2:[2]

You could create a helper function, no?


export function wrappedWaitFor(cb, opts = {}) {
  waitFor(cb, { ...opts, timeout: 10000 });
}

Solution 3:[3]

Adding to @jonrsharpe's answer https://stackoverflow.com/a/68253478/5015848

You can configure the timeout of async methods for @testing-library/react & @testing-library/dom in a setupTests.js / setupTests.ts file:

import { configure as configureReact } from "@testing-library/react";
import { configure as configureDom } from "@testing-library/dom";

configureReact({ asyncUtilTimeout: 5000 });
configureDom({ asyncUtilTimeout: 5000 });

But if you need to configure it for async methods of any other testing-library package then that needs to be configured for each async method, for ex.

waitFor(callback_func, { timeout: 5000 });

or create a helper function as suggested by @Baruch https://stackoverflow.com/a/68251268/5015848

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jonrsharpe
Solution 2 Baruch
Solution 3