Home / Function/ waitFor() — react Function Reference

waitFor() — react Function Reference

Architecture documentation for the waitFor() function in ReactInternalTestUtils.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  838adf3d_d2af_c196_5f88_59481e7d1a6c["waitFor()"]
  29c464eb_52d1_465c_432a_544e82213726["ReactInternalTestUtils.js"]
  838adf3d_d2af_c196_5f88_59481e7d1a6c -->|defined in| 29c464eb_52d1_465c_432a_544e82213726
  649f12be_3313_b4be_bac2_9cdf587b5e58["assertYieldsWereCleared()"]
  838adf3d_d2af_c196_5f88_59481e7d1a6c -->|calls| 649f12be_3313_b4be_bac2_9cdf587b5e58
  a171b128_18a5_9e04_9a7e_e896f8e81a4b["waitForMicrotasks()"]
  838adf3d_d2af_c196_5f88_59481e7d1a6c -->|calls| a171b128_18a5_9e04_9a7e_e896f8e81a4b
  style 838adf3d_d2af_c196_5f88_59481e7d1a6c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/internal-test-utils/ReactInternalTestUtils.js lines 43–90

export async function waitFor(expectedLog, options) {
  assertYieldsWereCleared(waitFor);

  // Create the error object before doing any async work, to get a better
  // stack trace.
  const error = new Error();
  Error.captureStackTrace(error, waitFor);

  const stopAfter = expectedLog.length;
  const actualLog = [];
  do {
    // Wait until end of current task/microtask.
    await waitForMicrotasks();
    if (SchedulerMock.unstable_hasPendingWork()) {
      SchedulerMock.unstable_flushNumberOfYields(stopAfter - actualLog.length);
      actualLog.push(...SchedulerMock.unstable_clearLog());
      if (stopAfter > actualLog.length) {
        // Continue flushing until we've logged the expected number of items.
      } else {
        // Once we've reached the expected sequence, wait one more microtask to
        // flush any remaining synchronous work.
        await waitForMicrotasks();
        actualLog.push(...SchedulerMock.unstable_clearLog());
        break;
      }
    } else {
      // There's no pending work, even after a microtask.
      break;
    }
  } while (true);

  if (options && options.additionalLogsAfterAttemptingToYield) {
    expectedLog = expectedLog.concat(
      options.additionalLogsAfterAttemptingToYield,
    );
  }

  if (equals(actualLog, expectedLog)) {
    return;
  }

  error.message = `
Expected sequence of events did not occur.

${diff(expectedLog, actualLog)}
`;
  throw error;
}

Domain

Subdomains

Frequently Asked Questions

What does waitFor() do?
waitFor() is a function in the react codebase, defined in packages/internal-test-utils/ReactInternalTestUtils.js.
Where is waitFor() defined?
waitFor() is defined in packages/internal-test-utils/ReactInternalTestUtils.js at line 43.
What does waitFor() call?
waitFor() calls 2 function(s): assertYieldsWereCleared, waitForMicrotasks.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free