@@ -363,31 +363,45 @@ describe('Storage API', () => {
363363describe ( 'Custom JWT' , ( ) => {
364364 describe ( 'Realtime' , ( ) => {
365365 test ( 'will connect with a properly signed jwt token' , async ( ) => {
366- const jwtToken = sign ( { sub : '1234567890' } , JWT_SECRET , { expiresIn : '1h' } )
366+ const jwtToken = sign (
367+ {
368+ sub : '1234567890' ,
369+ role : 'anon' ,
370+ iss : 'supabase-demo' ,
371+ } ,
372+ JWT_SECRET ,
373+ { expiresIn : '1h' }
374+ )
367375 const supabaseWithCustomJwt = createClient ( SUPABASE_URL , ANON_KEY , {
368376 accessToken : ( ) => Promise . resolve ( jwtToken ) ,
377+ realtime : {
378+ ...( wsTransport && { transport : wsTransport } ) ,
379+ } ,
369380 } )
370381
371- // Wait for subscription using Promise to avoid polling
372- await new Promise < void > ( ( resolve , reject ) => {
373- const timeout = setTimeout ( ( ) => {
374- reject ( new Error ( 'Timeout waiting for subscription' ) )
375- } , 10000 )
376-
377- supabaseWithCustomJwt . channel ( 'test-channel' ) . subscribe ( ( status , err ) => {
378- if ( status === 'SUBSCRIBED' ) {
379- clearTimeout ( timeout )
380- // Verify token was set
381- expect ( supabaseWithCustomJwt . realtime . accessTokenValue ) . toBe ( jwtToken )
382- resolve ( )
383- } else if ( status === 'CHANNEL_ERROR' || status === 'TIMED_OUT' ) {
384- clearTimeout ( timeout )
385- reject ( err || new Error ( `Subscription failed with status: ${ status } ` ) )
386- }
382+ try {
383+ // Wait for subscription using Promise to avoid polling
384+ await new Promise < void > ( ( resolve , reject ) => {
385+ const timeout = setTimeout ( ( ) => {
386+ reject ( new Error ( 'Timeout waiting for subscription' ) )
387+ } , 10000 )
388+
389+ supabaseWithCustomJwt . channel ( 'test-channel' ) . subscribe ( ( status , err ) => {
390+ if ( status === 'SUBSCRIBED' ) {
391+ clearTimeout ( timeout )
392+ // Verify token was set
393+ expect ( supabaseWithCustomJwt . realtime . accessTokenValue ) . toBe ( jwtToken )
394+ resolve ( )
395+ } else if ( status === 'CHANNEL_ERROR' || status === 'TIMED_OUT' ) {
396+ clearTimeout ( timeout )
397+ reject ( err || new Error ( `Subscription failed with status: ${ status } ` ) )
398+ }
399+ } )
387400 } )
388- } )
389-
390- await supabaseWithCustomJwt . removeAllChannels ( )
401+ } finally {
402+ // Always cleanup channels and connection, even if test fails
403+ await supabaseWithCustomJwt . removeAllChannels ( )
404+ }
391405 } , 15000 )
392406 } )
393407} )
0 commit comments