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

Function act

packages/internal-test-utils/internalAct.js:43–196  ·  view source on GitHub ↗
(scope: () => Thenable<T>)

Source from the content-addressed store, hash-verified

41}
42
43export async function act<T>(scope: () => Thenable<T>): Thenable<T> {
44 if (Scheduler.unstable_flushUntilNextPaint === undefined) {
45 throw Error(
46 'This version of `act` requires a special mock build of Scheduler.',
47 );
48 }
49
50 const actualYields = Scheduler.unstable_clearLog();
51 if (actualYields.length !== 0) {
52 const error = Error(
53 'Log of yielded values is not empty. Call assertLog first.\n\n' +
54 `Received:\n${diff('', actualYields.join('\n'), {
55 omitAnnotationLines: true,
56 })}`,
57 );
58 Error.captureStackTrace(error, act);
59 throw error;
60 }
61
62 // We require every `act` call to assert console logs
63 // with one of the assertion helpers. Fails if not empty.
64 assertConsoleLogsCleared();
65
66 // $FlowFixMe[cannot-resolve-name]: Flow doesn't know about global Jest object
67 if (!jest.isMockFunction(setTimeout)) {
68 throw Error(
69 "This version of `act` requires Jest's timer mocks " +
70 '(i.e. jest.useFakeTimers).',
71 );
72 }
73
74 const previousIsActEnvironment = global.IS_REACT_ACT_ENVIRONMENT;
75 const previousActingUpdatesScopeDepth = actingUpdatesScopeDepth;
76 actingUpdatesScopeDepth++;
77 if (actingUpdatesScopeDepth === 1) {
78 // Because this is not the "real" `act`, we set this to `false` so React
79 // knows not to fire `act` warnings.
80 global.IS_REACT_ACT_ENVIRONMENT = false;
81 }
82
83 // Create the error object before doing any async work, to get a better
84 // stack trace.
85 const error = new Error();
86 Error.captureStackTrace(error, act);
87
88 // Call the provided scope function after an async gap. This is an extra
89 // precaution to ensure that our tests do not accidentally rely on the act
90 // scope adding work to the queue synchronously. We don't do this in the
91 // public version of `act`, though we maybe should in the future.
92 await waitForMicrotasks();
93
94 const errorHandlerDOM = function (event: ErrorEvent) {
95 // Prevent logs from reprinting this error.
96 event.preventDefault();
97 thrownErrors.push(event.error);
98 };
99 const errorHandlerNode = function (err: mixed) {
100 thrownErrors.push(err);

Calls 9

assertConsoleLogsClearedFunction · 0.90
joinMethod · 0.80
onMethod · 0.80
runAllTicksMethod · 0.80
runOnlyPendingTimersMethod · 0.80
offMethod · 0.80
waitForMicrotasksFunction · 0.70
aggregateErrorsFunction · 0.70
diffFunction · 0.50

Tested by

no test coverage detected