11import { assertEquals , assertExists } from 'https://deno.land/[email protected] /assert/mod.ts' 2- import { createClient , SupabaseClient } from 'jsr:@supabase/supabase-js@2'
3- import type { RealtimeChannel } from 'jsr:@supabase/supabase-js@2'
2+ import { createClient , SupabaseClient } from 'jsr:@supabase/supabase-js@2.50.1-next.3 '
3+ import type { RealtimeChannel } from 'jsr:@supabase/supabase-js@2.50.1-next.3 '
44
55// These tests assume that a local Supabase server is already running
66// Start a local Supabase instance with 'supabase start' before running these tests
@@ -14,114 +14,125 @@ Deno.test('Supabase Integration Tests', async (t) => {
1414 realtime : { heartbeatIntervalMs : 500 } ,
1515 } )
1616
17- await t . step ( 'should connect to Supabase instance' , ( ) => {
18- assertExists ( supabase )
19- assertEquals ( supabase instanceof SupabaseClient , true )
20- } )
21-
22- await t . step ( 'PostgREST - should query data from public schema' , async ( ) => {
23- const { data, error } = await supabase . from ( 'todos' ) . select ( '*' ) . limit ( 5 )
24-
25- // The default schema includes a 'todos' table, but it might be empty
26- assertEquals ( error , null )
27- assertEquals ( Array . isArray ( data ) , true )
28- } )
17+ // Cleanup function to be called after all tests
18+ const cleanup = async ( ) => {
19+ await supabase . auth . signOut ( )
20+ await supabase . auth . stopAutoRefresh ( )
21+ await supabase . removeAllChannels ( )
22+ // Give some time for cleanup to complete
23+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) )
24+ }
25+
26+ try {
27+ await t . step ( 'should connect to Supabase instance' , ( ) => {
28+ assertExists ( supabase )
29+ assertEquals ( supabase instanceof SupabaseClient , true )
30+ } )
2931
30- await t . step ( 'PostgREST - should create and delete a todo' , async ( ) => {
31- // Create a new todo
32- const { data : createdTodo , error : createError } = await supabase
33- . from ( 'todos' )
34- . insert ( { task : 'Integration Test Todo' , is_complete : false } )
35- . select ( )
36- . single ( )
37-
38- assertEquals ( createError , null )
39- assertExists ( createdTodo )
40- assertEquals ( createdTodo ! . task , 'Integration Test Todo' )
41- assertEquals ( createdTodo ! . is_complete , false )
42-
43- // Delete the created todo
44- const { error : deleteError } = await supabase . from ( 'todos' ) . delete ( ) . eq ( 'id' , createdTodo ! . id )
45-
46- assertEquals ( deleteError , null )
47-
48- // Verify the todo was deleted
49- const { data : fetchedTodo , error : fetchError } = await supabase
50- . from ( 'todos' )
51- . select ( '*' )
52- . eq ( 'id' , createdTodo ! . id )
53- . single ( )
54-
55- assertExists ( fetchError )
56- assertEquals ( fetchedTodo , null )
57- } )
32+ await t . step ( 'PostgREST - should query data from public schema' , async ( ) => {
33+ const { data, error } = await supabase . from ( 'todos' ) . select ( '*' ) . limit ( 5 )
5834
59- await t . step ( 'Authentication - should sign up a user' , async ( ) => {
60- const email = `test-${ Date . now ( ) } @example.com`
61- const password = 'password123'
35+ // The default schema includes a 'todos' table, but it might be empty
36+ assertEquals ( error , null )
37+ assertEquals ( Array . isArray ( data ) , true )
38+ } )
6239
63- const { data, error } = await supabase . auth . signUp ( {
64- email,
65- password,
40+ await t . step ( 'PostgREST - should create and delete a todo' , async ( ) => {
41+ // Create a new todo
42+ const { data : createdTodo , error : createError } = await supabase
43+ . from ( 'todos' )
44+ . insert ( { task : 'Integration Test Todo' , is_complete : false } )
45+ . select ( )
46+ . single ( )
47+
48+ assertEquals ( createError , null )
49+ assertExists ( createdTodo )
50+ assertEquals ( createdTodo ! . task , 'Integration Test Todo' )
51+ assertEquals ( createdTodo ! . is_complete , false )
52+
53+ // Delete the created todo
54+ const { error : deleteError } = await supabase . from ( 'todos' ) . delete ( ) . eq ( 'id' , createdTodo ! . id )
55+
56+ assertEquals ( deleteError , null )
57+
58+ // Verify the todo was deleted
59+ const { data : fetchedTodo , error : fetchError } = await supabase
60+ . from ( 'todos' )
61+ . select ( '*' )
62+ . eq ( 'id' , createdTodo ! . id )
63+ . single ( )
64+
65+ assertExists ( fetchError )
66+ assertEquals ( fetchedTodo , null )
6667 } )
6768
68- assertEquals ( error , null )
69- assertExists ( data . user )
70- assertEquals ( data . user ! . email , email )
69+ await t . step ( 'Authentication - should sign up a user' , async ( ) => {
70+ const email = `test- ${ Date . now ( ) } @example.com`
71+ const password = 'password123'
7172
72- // Clean up by signing out the user
73- await supabase . auth . signOut ( )
74- } )
75-
76- await t . step ( 'Realtime - is able to connect and broadcast' , async ( ) => {
77- const channelName = `channel-${ crypto . randomUUID ( ) } `
78- let channel : RealtimeChannel
79- const email = `test-${ Date . now ( ) } @example.com`
80- const password = 'password123'
81-
82- // Sign up and create channel
83- await supabase . auth . signUp ( { email, password } )
84- const config = { broadcast : { self : true } , private : true }
85- channel = supabase . channel ( channelName , { config } )
86- await supabase . realtime . setAuth ( )
87-
88- const testMessage = { message : 'test' }
89- let receivedMessage : any
90- let subscribed = false
91- let attempts = 0
92-
93- channel
94- . on ( 'broadcast' , { event : '*' } , ( payload : unknown ) => ( receivedMessage = payload ) )
95- . subscribe ( ( status : string ) => {
96- if ( status == 'SUBSCRIBED' ) subscribed = true
73+ const { data, error } = await supabase . auth . signUp ( {
74+ email,
75+ password,
9776 } )
9877
99- // Wait for subscription
100- while ( ! subscribed ) {
101- if ( attempts > 50 ) throw new Error ( 'Timeout waiting for subscription' )
102- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
103- attempts ++
104- }
105-
106- attempts = 0
107-
108- channel . send ( {
109- type : 'broadcast' ,
110- event : 'test-event' ,
111- payload : testMessage ,
78+ assertEquals ( error , null )
79+ assertExists ( data . user )
80+ assertEquals ( data . user ! . email , email )
11281 } )
11382
114- // Wait on message
115- while ( ! receivedMessage ) {
116- if ( attempts > 50 ) throw new Error ( 'Timeout waiting for message' )
117- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
118- attempts ++
119- }
83+ await t . step ( 'Realtime - is able to connect and broadcast' , async ( ) => {
84+ const channelName = `channel-${ crypto . randomUUID ( ) } `
85+ let channel : RealtimeChannel
86+ const email = `test-${ Date . now ( ) } @example.com`
87+ const password = 'password123'
88+
89+ // Sign up and create channel
90+ await supabase . auth . signUp ( { email, password } )
91+ const config = { broadcast : { self : true } , private : true }
92+ channel = supabase . channel ( channelName , { config } )
93+ await supabase . realtime . setAuth ( )
94+
95+ const testMessage = { message : 'test' }
96+ let receivedMessage : any
97+ let subscribed = false
98+ let attempts = 0
99+
100+ channel
101+ . on ( 'broadcast' , { event : '*' } , ( payload : unknown ) => ( receivedMessage = payload ) )
102+ . subscribe ( ( status : string ) => {
103+ if ( status == 'SUBSCRIBED' ) subscribed = true
104+ } )
105+
106+ // Wait for subscription
107+ while ( ! subscribed ) {
108+ if ( attempts > 50 ) throw new Error ( 'Timeout waiting for subscription' )
109+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
110+ attempts ++
111+ }
112+
113+ attempts = 0
114+
115+ channel . send ( {
116+ type : 'broadcast' ,
117+ event : 'test-event' ,
118+ payload : testMessage ,
119+ } )
120120
121- assertExists ( receivedMessage )
122- assertEquals ( supabase . realtime . getChannels ( ) . length , 1 )
121+ // Wait on message
122+ while ( ! receivedMessage ) {
123+ if ( attempts > 50 ) throw new Error ( 'Timeout waiting for message' )
124+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
125+ attempts ++
126+ }
123127
124- // Cleanup
125- await supabase . removeAllChannels ( )
126- } )
128+ assertExists ( receivedMessage )
129+ assertEquals ( supabase . realtime . getChannels ( ) . length , 1 )
130+
131+ // Cleanup channel
132+ await channel . unsubscribe ( )
133+ } )
134+ } finally {
135+ // Ensure cleanup runs even if tests fail
136+ await cleanup ( )
137+ }
127138} )
0 commit comments