Skip to content

Commit 8628fba

Browse files
committed
[Internal API only] Delete non-awaited form of act
**This commit only affects the internal version of `act` that we use in this repo. The public `act` API is unaffected, for now.** We should always await the result of an `act` call so that any work queued in a microtask has a chance to flush. Neglecting to do this can cause us to miss bugs when testing React behavior. I codemodded all the existing `act` callers in previous PRs.
1 parent 8b8eb8e commit 8628fba

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

Diff for: packages/jest-react/src/internalAct.js

+32-44
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import enqueueTask from 'shared/enqueueTask';
2222

2323
let actingUpdatesScopeDepth = 0;
2424

25-
export function act<T>(scope: () => Thenable<T> | T): Thenable<T> {
25+
export function act<T>(scope: () => Thenable<T>): Thenable<T> {
2626
if (Scheduler.unstable_flushUntilNextPaint === undefined) {
2727
throw Error(
2828
'This version of `act` requires a special mock build of Scheduler.',
@@ -70,52 +70,40 @@ export function act<T>(scope: () => Thenable<T> | T): Thenable<T> {
7070
try {
7171
const result = scope();
7272
if (
73-
typeof result === 'object' &&
7473
result !== null &&
75-
// $FlowFixMe[method-unbinding]
76-
typeof result.then === 'function'
74+
typeof result === 'object' &&
75+
typeof (result: any).then === 'function'
7776
) {
78-
const thenableResult: Thenable<T> = (result: any);
79-
return {
80-
then(resolve: T => mixed, reject: mixed => mixed) {
81-
thenableResult.then(
82-
returnValue => {
83-
flushActWork(
84-
() => {
85-
unwind();
86-
resolve(returnValue);
87-
},
88-
error => {
89-
unwind();
90-
reject(error);
91-
},
92-
);
93-
},
94-
error => {
95-
unwind();
96-
reject(error);
97-
},
98-
);
99-
},
100-
};
101-
} else {
102-
const returnValue: T = (result: any);
103-
try {
104-
// TODO: Let's not support non-async scopes at all in our tests. Need to
105-
// migrate existing tests.
106-
let didFlushWork;
107-
do {
108-
didFlushWork = Scheduler.unstable_flushAllWithoutAsserting();
109-
} while (didFlushWork);
110-
return {
111-
then(resolve: T => mixed, reject: mixed => mixed) {
112-
resolve(returnValue);
113-
},
114-
};
115-
} finally {
116-
unwind();
117-
}
77+
throw new Error(
78+
'The internal version of `act` used in the React repo must be passed ' +
79+
"an async function, even if doesn't await anything. This is a " +
80+
'temporary limitation that will soon be fixed.',
81+
);
11882
}
83+
const thenableResult: Thenable<T> = (result: any);
84+
85+
return {
86+
then(resolve: T => mixed, reject: mixed => mixed) {
87+
thenableResult.then(
88+
returnValue => {
89+
flushActWork(
90+
() => {
91+
unwind();
92+
resolve(returnValue);
93+
},
94+
error => {
95+
unwind();
96+
reject(error);
97+
},
98+
);
99+
},
100+
error => {
101+
unwind();
102+
reject(error);
103+
},
104+
);
105+
},
106+
};
119107
} catch (error) {
120108
unwind();
121109
throw error;

0 commit comments

Comments
 (0)