11import { AsyncLocalStorage } from 'node:async_hooks' ;
22
33import { LogEventType } from '../../constants/internal' ;
4+ import { getTestRunPromise } from '../../context/testRunPromise' ;
45import { getPlaywrightPage } from '../../useContext' ;
56import { getFullPackConfig } from '../../utils/config' ;
67import { setCustomInspectOnFunction } from '../../utils/fn' ;
@@ -39,15 +40,16 @@ export const waitForResponse = (async <
3940 SomeResponse extends Response = Response ,
4041> (
4142 predicate : ResponsePredicate < SomeRequest , SomeResponse > ,
42- triggerOrOptions ?: Options | Trigger ,
43+ triggerOrOptions ?: Options | Trigger | undefined ,
4344 options ?: Options ,
4445) : Promise < ResponseWithRequest < SomeRequest , SomeResponse > > => {
4546 const startTimeInMs = Date . now ( ) as UtcTimeInMs ;
4647
4748 setCustomInspectOnFunction ( predicate ) ;
4849
4950 const trigger = typeof triggerOrOptions === 'function' ? triggerOrOptions : undefined ;
50- const finalOptions = typeof triggerOrOptions === 'function' ? options : triggerOrOptions ;
51+ const finalOptions =
52+ typeof triggerOrOptions === 'function' ? options : ( triggerOrOptions ?? options ) ;
5153
5254 const timeout = finalOptions ?. timeout ?? getFullPackConfig ( ) . waitForResponseTimeout ;
5355
@@ -56,6 +58,13 @@ export const waitForResponse = (async <
5658 }
5759
5860 const page = getPlaywrightPage ( ) ;
61+ const testRunPromise = getTestRunPromise ( ) ;
62+
63+ let isTestRunCompleted = false ;
64+
65+ void testRunPromise . then ( ( ) => {
66+ isTestRunCompleted = true ;
67+ } ) ;
5968
6069 const promise = page
6170 . waitForResponse (
@@ -73,7 +82,14 @@ export const waitForResponse = (async <
7382 getResponseFromPlaywrightResponse ( playwrightResponse ) as Promise <
7483 ResponseWithRequest < SomeRequest , SomeResponse >
7584 > ,
76- ) ;
85+ )
86+ . catch ( ( error : unknown ) => {
87+ if ( isTestRunCompleted ) {
88+ return new Promise < ResponseWithRequest < SomeRequest , SomeResponse > > ( ( ) => { } ) ;
89+ }
90+
91+ throw error ;
92+ } ) ;
7793
7894 const timeoutWithUnits = getDurationWithUnits ( timeout ) ;
7995
0 commit comments