99
1010let React ;
1111let Scheduler ;
12+ let waitForAll ;
13+ let assertLog ;
1214let ReactNoop ;
1315let useState ;
1416let act ;
@@ -32,6 +34,10 @@ describe('act warnings', () => {
3234 startTransition = React . startTransition ;
3335 getCacheForType = React . unstable_getCacheForType ;
3436 caches = [ ] ;
37+
38+ const InternalTestUtils = require ( 'internal-test-utils' ) ;
39+ waitForAll = InternalTestUtils . waitForAll ;
40+ assertLog = InternalTestUtils . assertLog ;
3541 } ) ;
3642
3743 function createTextCache ( ) {
@@ -134,17 +140,17 @@ describe('act warnings', () => {
134140 }
135141 }
136142
137- function withActEnvironment ( value , scope ) {
143+ async function withActEnvironment ( value , scope ) {
138144 const prevValue = global . IS_REACT_ACT_ENVIRONMENT ;
139145 global . IS_REACT_ACT_ENVIRONMENT = value ;
140146 try {
141- return scope ( ) ;
147+ return await scope ( ) ;
142148 } finally {
143149 global . IS_REACT_ACT_ENVIRONMENT = prevValue ;
144150 }
145151 }
146152
147- test ( 'warns about unwrapped updates only if environment flag is enabled' , ( ) => {
153+ test ( 'warns about unwrapped updates only if environment flag is enabled' , async ( ) => {
148154 let setState ;
149155 function App ( ) {
150156 const [ state , _setState ] = useState ( 0 ) ;
@@ -154,34 +160,34 @@ describe('act warnings', () => {
154160
155161 const root = ReactNoop . createRoot ( ) ;
156162 root . render ( < App /> ) ;
157- expect ( Scheduler ) . toFlushAndYield ( [ 0 ] ) ;
163+ await waitForAll ( [ 0 ] ) ;
158164 expect ( root ) . toMatchRenderedOutput ( '0' ) ;
159165
160166 // Default behavior. Flag is undefined. No warning.
161167 expect ( global . IS_REACT_ACT_ENVIRONMENT ) . toBe ( undefined ) ;
162168 setState ( 1 ) ;
163- expect ( Scheduler ) . toFlushAndYield ( [ 1 ] ) ;
169+ await waitForAll ( [ 1 ] ) ;
164170 expect ( root ) . toMatchRenderedOutput ( '1' ) ;
165171
166172 // Flag is true. Warn.
167- withActEnvironment ( true , ( ) => {
173+ await withActEnvironment ( true , async ( ) => {
168174 expect ( ( ) => setState ( 2 ) ) . toErrorDev (
169175 'An update to App inside a test was not wrapped in act' ,
170176 ) ;
171- expect ( Scheduler ) . toFlushAndYield ( [ 2 ] ) ;
177+ await waitForAll ( [ 2 ] ) ;
172178 expect ( root ) . toMatchRenderedOutput ( '2' ) ;
173179 } ) ;
174180
175181 // Flag is false. No warning.
176- withActEnvironment ( false , ( ) => {
182+ await withActEnvironment ( false , async ( ) => {
177183 setState ( 3 ) ;
178- expect ( Scheduler ) . toFlushAndYield ( [ 3 ] ) ;
184+ await waitForAll ( [ 3 ] ) ;
179185 expect ( root ) . toMatchRenderedOutput ( '3' ) ;
180186 } ) ;
181187 } ) ;
182188
183189 // @gate __DEV__
184- test ( 'act warns if the environment flag is not enabled' , ( ) => {
190+ test ( 'act warns if the environment flag is not enabled' , async ( ) => {
185191 let setState ;
186192 function App ( ) {
187193 const [ state , _setState ] = useState ( 0 ) ;
@@ -191,7 +197,7 @@ describe('act warnings', () => {
191197
192198 const root = ReactNoop . createRoot ( ) ;
193199 root . render ( < App /> ) ;
194- expect ( Scheduler ) . toFlushAndYield ( [ 0 ] ) ;
200+ await waitForAll ( [ 0 ] ) ;
195201 expect ( root ) . toMatchRenderedOutput ( '0' ) ;
196202
197203 // Default behavior. Flag is undefined. Warn.
@@ -204,20 +210,20 @@ describe('act warnings', () => {
204210 'The current testing environment is not configured to support act(...)' ,
205211 { withoutStack : true } ,
206212 ) ;
207- expect ( Scheduler ) . toHaveYielded ( [ 1 ] ) ;
213+ assertLog ( [ 1 ] ) ;
208214 expect ( root ) . toMatchRenderedOutput ( '1' ) ;
209215
210216 // Flag is true. Don't warn.
211- withActEnvironment ( true , ( ) => {
217+ await withActEnvironment ( true , ( ) => {
212218 act ( ( ) => {
213219 setState ( 2 ) ;
214220 } ) ;
215- expect ( Scheduler ) . toHaveYielded ( [ 2 ] ) ;
221+ assertLog ( [ 2 ] ) ;
216222 expect ( root ) . toMatchRenderedOutput ( '2' ) ;
217223 } ) ;
218224
219225 // Flag is false. Warn.
220- withActEnvironment ( false , ( ) => {
226+ await withActEnvironment ( false , ( ) => {
221227 expect ( ( ) => {
222228 act ( ( ) => {
223229 setState ( 1 ) ;
@@ -226,13 +232,13 @@ describe('act warnings', () => {
226232 'The current testing environment is not configured to support act(...)' ,
227233 { withoutStack : true } ,
228234 ) ;
229- expect ( Scheduler ) . toHaveYielded ( [ 1 ] ) ;
235+ assertLog ( [ 1 ] ) ;
230236 expect ( root ) . toMatchRenderedOutput ( '1' ) ;
231237 } ) ;
232238 } ) ;
233239
234- test ( 'warns if root update is not wrapped' , ( ) => {
235- withActEnvironment ( true , ( ) => {
240+ test ( 'warns if root update is not wrapped' , async ( ) => {
241+ await withActEnvironment ( true , ( ) => {
236242 const root = ReactNoop . createRoot ( ) ;
237243 expect ( ( ) => root . render ( 'Hi' ) ) . toErrorDev (
238244 // TODO: Better error message that doesn't make it look like "Root" is
@@ -244,7 +250,7 @@ describe('act warnings', () => {
244250 } ) ;
245251
246252 // @gate __DEV__
247- test ( 'warns if class update is not wrapped' , ( ) => {
253+ test ( 'warns if class update is not wrapped' , async ( ) => {
248254 let app ;
249255 class App extends React . Component {
250256 state = { count : 0 } ;
@@ -254,7 +260,7 @@ describe('act warnings', () => {
254260 }
255261 }
256262
257- withActEnvironment ( true , ( ) => {
263+ await withActEnvironment ( true , ( ) => {
258264 const root = ReactNoop . createRoot ( ) ;
259265 act ( ( ) => {
260266 root . render ( < App /> ) ;
@@ -266,18 +272,18 @@ describe('act warnings', () => {
266272 } ) ;
267273
268274 // @gate __DEV__
269- test ( 'warns even if update is synchronous' , ( ) => {
275+ test ( 'warns even if update is synchronous' , async ( ) => {
270276 let setState ;
271277 function App ( ) {
272278 const [ state , _setState ] = useState ( 0 ) ;
273279 setState = _setState ;
274280 return < Text text = { state } /> ;
275281 }
276282
277- withActEnvironment ( true , ( ) => {
283+ await withActEnvironment ( true , ( ) => {
278284 const root = ReactNoop . createRoot ( ) ;
279285 act ( ( ) => root . render ( < App /> ) ) ;
280- expect ( Scheduler ) . toHaveYielded ( [ 0 ] ) ;
286+ assertLog ( [ 0 ] ) ;
281287 expect ( root ) . toMatchRenderedOutput ( '0' ) ;
282288
283289 // Even though this update is synchronous, we should still fire a warning,
@@ -286,14 +292,14 @@ describe('act warnings', () => {
286292 'An update to App inside a test was not wrapped in act(...)' ,
287293 ) ;
288294
289- expect ( Scheduler ) . toHaveYielded ( [ 1 ] ) ;
295+ assertLog ( [ 1 ] ) ;
290296 expect ( root ) . toMatchRenderedOutput ( '1' ) ;
291297 } ) ;
292298 } ) ;
293299
294300 // @gate __DEV__
295301 // @gate enableLegacyCache
296- test ( 'warns if Suspense retry is not wrapped' , ( ) => {
302+ test ( 'warns if Suspense retry is not wrapped' , async ( ) => {
297303 function App ( ) {
298304 return (
299305 < Suspense fallback = { < Text text = "Loading..." /> } >
@@ -302,12 +308,12 @@ describe('act warnings', () => {
302308 ) ;
303309 }
304310
305- withActEnvironment ( true , ( ) => {
311+ await withActEnvironment ( true , ( ) => {
306312 const root = ReactNoop . createRoot ( ) ;
307313 act ( ( ) => {
308314 root . render ( < App /> ) ;
309315 } ) ;
310- expect ( Scheduler ) . toHaveYielded ( [ 'Suspend! [Async]' , 'Loading...' ] ) ;
316+ assertLog ( [ 'Suspend! [Async]' , 'Loading...' ] ) ;
311317 expect ( root ) . toMatchRenderedOutput ( 'Loading...' ) ;
312318
313319 // This is a retry, not a ping, because we already showed a fallback.
@@ -321,7 +327,7 @@ describe('act warnings', () => {
321327
322328 // @gate __DEV__
323329 // @gate enableLegacyCache
324- test ( 'warns if Suspense ping is not wrapped' , ( ) => {
330+ test ( 'warns if Suspense ping is not wrapped' , async ( ) => {
325331 function App ( { showMore} ) {
326332 return (
327333 < Suspense fallback = { < Text text = "Loading..." /> } >
@@ -330,20 +336,20 @@ describe('act warnings', () => {
330336 ) ;
331337 }
332338
333- withActEnvironment ( true , ( ) => {
339+ await withActEnvironment ( true , ( ) => {
334340 const root = ReactNoop . createRoot ( ) ;
335341 act ( ( ) => {
336342 root . render ( < App showMore = { false } /> ) ;
337343 } ) ;
338- expect ( Scheduler ) . toHaveYielded ( [ '(empty)' ] ) ;
344+ assertLog ( [ '(empty)' ] ) ;
339345 expect ( root ) . toMatchRenderedOutput ( '(empty)' ) ;
340346
341347 act ( ( ) => {
342348 startTransition ( ( ) => {
343349 root . render ( < App showMore = { true } /> ) ;
344350 } ) ;
345351 } ) ;
346- expect ( Scheduler ) . toHaveYielded ( [ 'Suspend! [Async]' , 'Loading...' ] ) ;
352+ assertLog ( [ 'Suspend! [Async]' , 'Loading...' ] ) ;
347353 expect ( root ) . toMatchRenderedOutput ( '(empty)' ) ;
348354
349355 // This is a ping, not a retry, because no fallback is showing.
0 commit comments