@@ -10,6 +10,25 @@ const tick = async () => {
1010 await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
1111}
1212
13+ const waitForExpectation = async ( assertion : ( ) => void , timeoutMs = 1_000 ) : Promise < void > => {
14+ const deadline = Date . now ( ) + timeoutMs
15+ let lastError : unknown
16+
17+ while ( Date . now ( ) < deadline ) {
18+ try {
19+ assertion ( )
20+ return
21+ } catch ( error ) {
22+ lastError = error
23+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
24+ }
25+ }
26+
27+ throw lastError instanceof Error
28+ ? lastError
29+ : new Error ( 'Timed out waiting for asynchronous expectation.' )
30+ }
31+
1332afterEach ( ( ) => {
1433 document . body . innerHTML = ''
1534 __fictDisableSSR ( )
@@ -285,12 +304,12 @@ describe('reactify', () => {
285304 document . body . appendChild ( container )
286305
287306 const dispose = render ( ( ) => ( { type : CustomHost , props : { value : 'ok' } } ) , container )
288- await tick ( )
289-
290- const host = container . querySelector ( '[data-fict-react- host]' ) as HTMLElement | null
291- expect ( host ) . not . toBeNull ( )
292- expect ( host ?. tagName ) . toBe ( 'SECTION ' )
293- expect ( container . querySelector ( '#custom-host' ) ?. textContent ) . toBe ( 'ok' )
307+ await waitForExpectation ( ( ) => {
308+ const host = container . querySelector ( '[data-fict-react-host]' ) as HTMLElement | null
309+ expect ( host ) . not . toBeNull ( )
310+ expect ( host ?. tagName ) . toBe ( 'SECTION' )
311+ expect ( container . querySelector ( '#custom- host' ) ?. textContent ) . toBe ( 'ok ' )
312+ } )
294313
295314 dispose ( )
296315 } )
@@ -333,13 +352,13 @@ describe('ReactIsland', () => {
333352 document . body . appendChild ( container )
334353
335354 const dispose = render ( ( ) => ( { type : App , props : { } } ) , container )
336- await tick ( )
337-
338- expect ( container . querySelector ( '#island-label' ) ?. textContent ) . toBe ( 'alpha' )
355+ await waitForExpectation ( ( ) => {
356+ expect ( container . querySelector ( '#island-label' ) ?. textContent ) . toBe ( 'alpha' )
357+ } )
339358 ; ( container . querySelector ( '#swap' ) as HTMLButtonElement ) . click ( )
340- await tick ( )
341-
342- expect ( container . querySelector ( '#island-label' ) ?. textContent ) . toBe ( 'beta' )
359+ await waitForExpectation ( ( ) => {
360+ expect ( container . querySelector ( '#island-label' ) ?. textContent ) . toBe ( 'beta' )
361+ } )
343362
344363 dispose ( )
345364 } )
@@ -363,12 +382,12 @@ describe('ReactIsland', () => {
363382 } ) ,
364383 container ,
365384 )
366- await tick ( )
367-
368- const host = container . querySelector ( '[data-fict-react- host]' ) as HTMLElement | null
369- expect ( host ) . not . toBeNull ( )
370- expect ( host ?. tagName ) . toBe ( 'ARTICLE ' )
371- expect ( container . querySelector ( '#island-custom-host' ) ?. textContent ) . toBe ( 'custom' )
385+ await waitForExpectation ( ( ) => {
386+ const host = container . querySelector ( '[data-fict-react-host]' ) as HTMLElement | null
387+ expect ( host ) . not . toBeNull ( )
388+ expect ( host ?. tagName ) . toBe ( 'ARTICLE' )
389+ expect ( container . querySelector ( '#island-custom- host' ) ?. textContent ) . toBe ( 'custom ' )
390+ } )
372391
373392 dispose ( )
374393 } )
0 commit comments