Testing Asynchronous Components with findBy and waitFor
Learn how to correctly test components that update after an asynchronous operation, without relying on arbitrary timeouts.
Components that fetch data or otherwise update asynchronously (after a promise resolves, a timer fires, or a debounce settles) can't be tested with a synchronous assertion immediately after rendering or triggering an interaction, since the expected UI change hasn't happened yet at that exact moment. Testing Library provides two main tools for this: findBy* queries (which return a promise that resolves once a matching element appears, or rejects after a timeout) and waitFor (which repeatedly re-runs a callback until it stops throwing, or times out).
findBy/waitFor is like repeatedly and efficiently checking a mailbox every few minutes until a package actually arrives, versus deciding in advance to wait exactly one full day regardless of whether the package shows up in the first ten minutes or takes two days — the polling approach adapts to how long things actually take instead of guessing a fixed wait.