MCPcopy
hub / github.com/facebook/react / waitFor

Function waitFor

packages/internal-test-utils/ReactInternalTestUtils.js:43–90  ·  packages/internal-test-utils/ReactInternalTestUtils.js::waitFor
(expectedLog, options)

Source from the content-addressed store, hash-verified

41}
42
43export async function waitFor(expectedLog, options) {
44 assertYieldsWereCleared(waitFor);
45
46 class="cm">// Create the error object before doing any async work, to get a better
47 class="cm">// stack trace.
48 const error = new Error();
49 Error.captureStackTrace(error, waitFor);
50
51 const stopAfter = expectedLog.length;
52 const actualLog = [];
53 do {
54 class="cm">// Wait until end of current task/microtask.
55 await waitForMicrotasks();
56 if (SchedulerMock.unstable_hasPendingWork()) {
57 SchedulerMock.unstable_flushNumberOfYields(stopAfter - actualLog.length);
58 actualLog.push(...SchedulerMock.unstable_clearLog());
59 if (stopAfter > actualLog.length) {
60 class="cm">// Continue flushing until we've logged the expected number of items.
61 } else {
62 class="cm">// Once we've reached the expected sequence, wait one more microtask to
63 class="cm">// flush any remaining synchronous work.
64 await waitForMicrotasks();
65 actualLog.push(...SchedulerMock.unstable_clearLog());
66 break;
67 }
68 } else {
69 class="cm">// There's no pending work, even after a microtask.
70 break;
71 }
72 } while (true);
73
74 if (options && options.additionalLogsAfterAttemptingToYield) {
75 expectedLog = expectedLog.concat(
76 options.additionalLogsAfterAttemptingToYield,
77 );
78 }
79
80 if (equals(actualLog, expectedLog)) {
81 return;
82 }
83
84 error.message = `
85Expected sequence of events did not occur.
86
87${diff(expectedLog, actualLog)}
88`;
89 throw error;
90}
91
92export async function waitForAll(expectedLog) {
93 assertYieldsWereCleared(waitForAll);

Calls 4

assertYieldsWereClearedFunction · 0.70
waitForMicrotasksFunction · 0.70
pushMethod · 0.65
diffFunction · 0.50

Tested by 7

FooFunction · 0.68
CounterFunction · 0.68
ParentFunction · 0.68
OwnerStackDelayedFunction · 0.68
SiblingFunction · 0.68
AppFunction · 0.68
AppFunction · 0.68