Skip to content

Commit 91701bf

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 5565069 commit 91701bf

File tree

1 file changed

+33
-44
lines changed

1 file changed

+33
-44
lines changed

packages/jest-react/src/internalAct.js

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -69,53 +69,42 @@ export function act<T>(scope: () => Thenable<T> | T): Thenable<T> {
6969
// our test suite, we should be able to.
7070
try {
7171
const result = scope();
72+
const thenableResult: Thenable<T> = (result: any);
73+
7274
if (
73-
typeof result === 'object' &&
74-
result !== null &&
75-
// $FlowFixMe[method-unbinding]
76-
typeof result.then === 'function'
75+
typeof thenableResult !== 'object' ||
76+
thenableResult === null ||
77+
typeof thenableResult.then !== 'function'
7778
) {
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-
}
79+
throw new Error(
80+
'The internal version of `act` used in the React repo must be passed ' +
81+
"an async function, even if doesn't await anything. This is a " +
82+
'temporary limitation that will soon be fixed.',
83+
);
11884
}
85+
86+
return {
87+
then(resolve: T => mixed, reject: mixed => mixed) {
88+
thenableResult.then(
89+
returnValue => {
90+
flushActWork(
91+
() => {
92+
unwind();
93+
resolve(returnValue);
94+
},
95+
error => {
96+
unwind();
97+
reject(error);
98+
},
99+
);
100+
},
101+
error => {
102+
unwind();
103+
reject(error);
104+
},
105+
);
106+
},
107+
};
119108
} catch (error) {
120109
unwind();
121110
throw error;

0 commit comments

Comments
 (0)