@@ -17,6 +17,12 @@ const server = Bun.serve({
1717 const method = Reflect . get ( message , "method" )
1818 const id = Reflect . get ( message , "id" )
1919 if ( method !== "Page.navigate" || typeof id !== "number" ) return
20+ const params = Reflect . get ( message , "params" )
21+ const url = typeof params === "object" && params !== null ? Reflect . get ( params , "url" ) : undefined
22+ if ( url === "https://navigation-fails.example" ) {
23+ ws . send ( JSON . stringify ( { id, result : { frameId : "frame" , errorText : "net::ERR_FAILED" } } ) )
24+ return
25+ }
2026 ws . send ( JSON . stringify ( { method : "Page.loadEventFired" , params : { timestamp : 1 } } ) )
2127 ws . send ( JSON . stringify ( { id, result : { frameId : "frame" } } ) )
2228 } ,
@@ -34,8 +40,8 @@ afterAll(() => {
3440 server . stop ( true )
3541} )
3642
37- const emit = ( method : string , params : unknown ) => {
38- server . publish ( channel , JSON . stringify ( { method, params } ) )
43+ const emit = ( method : string , params : unknown , sessionId ?: string ) => {
44+ server . publish ( channel , JSON . stringify ( { method, params, sessionId } ) )
3945}
4046
4147test ( "waitFor accepts predicate and timeout options" , async ( ) => {
@@ -82,6 +88,26 @@ test("a waiter registered before navigation catches an event emitted before the
8288 expect ( await loaded ) . toEqual ( { timestamp : 1 } )
8389} )
8490
91+ test ( "waitFor ignores matching events from another attached session" , async ( ) => {
92+ session . setActiveSession ( "session-active" )
93+ try {
94+ const waiting = session . waitFor < { source : string } > ( "Page.loadEventFired" , { timeoutMs : 1_000 } )
95+ emit ( "Page.loadEventFired" , { source : "background" } , "session-background" )
96+ emit ( "Page.loadEventFired" , { source : "active" } , "session-active" )
97+ expect ( await waiting ) . toEqual ( { source : "active" } )
98+ } finally {
99+ session . setActiveSession ( undefined )
100+ }
101+ } )
102+
103+ test ( "navigation failure does not leave an unhandled waiter rejection" , async ( ) => {
104+ const loaded = session . waitFor ( "Page.loadEventFired" , { timeoutMs : 20 } )
105+ void loaded . catch ( ( ) => { } )
106+ const navigation = await session . domains . Page . navigate ( { url : "https://navigation-fails.example" } )
107+ expect ( navigation . errorText ) . toBe ( "net::ERR_FAILED" )
108+ await Bun . sleep ( 30 )
109+ } )
110+
85111test ( "waitFor rejects invalid runtime arguments immediately" , ( ) => {
86112 expect ( ( ) =>
87113 // @ts -expect-error Runtime callers can still pass invalid JavaScript.
0 commit comments