Skip to content

Commit 624f3b6

Browse files
committed
test(deno): add integration tests for deno
1 parent 146c822 commit 624f3b6

File tree

5 files changed

+154
-4
lines changed

5 files changed

+154
-4
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
matrix:
5454
os: [ubuntu-latest]
5555
node: ['20']
56-
deno: ['2.x']
56+
deno: ['1.x', '2.x']
5757

5858
runs-on: ${{ matrix.os }}
5959

@@ -78,12 +78,17 @@ jobs:
7878
run: |
7979
supabase start
8080
81-
- name: Run tests
81+
- name: Run Node.js tests
8282
run: |
8383
npm clean-install
84-
npm run test:integration || npm run test:integration
84+
npm run test:integration
8585
npm run test:integration:browser
8686
87+
- name: Run Deno tests
88+
run: |
89+
cd test/deno
90+
deno task test
91+
8792
- name: Stop Supabase
8893
run: |
8994
supabase stop

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"test": "run-s test:types test:run",
3232
"test:all": "run-s test:types test:run test:integration test:integration:browser",
3333
"test:run": "jest --runInBand --detectOpenHandles",
34-
"test:coverage": "jest --runInBand --coverage --testPathIgnorePatterns=\"test/integration.*\"",
34+
"test:coverage": "jest --runInBand --coverage --testPathIgnorePatterns=\"test/integration.*|test/deno.*\"",
3535
"test:integration": "jest --runInBand --detectOpenHandles test/integration.test.ts",
3636
"test:integration:browser": "deno test --allow-all test/integration.browser.test.ts",
3737
"test:db": "cd infra/db && docker-compose down && docker-compose up -d && sleep 5",

test/deno/deno.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["deno.window"],
4+
"strict": true
5+
},
6+
"imports": {
7+
"@supabase/supabase-js": "../../src/index.ts",
8+
"../../src/SupabaseClient": "../../src/SupabaseClient.ts",
9+
"../../src/lib/types": "../../src/lib/types.ts"
10+
},
11+
"tasks": {
12+
"test": "deno test --allow-net --allow-env --unstable-sloppy-imports integration.test.ts"
13+
}
14+
}

test/deno/integration.test.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { assertEquals, assertExists } from 'https://deno.land/[email protected]/assert/mod.ts'
2+
import { createClient, RealtimeChannel, SupabaseClient } from '@supabase/supabase-js'
3+
4+
// These tests assume that a local Supabase server is already running
5+
// Start a local Supabase instance with 'supabase start' before running these tests
6+
Deno.test('Supabase Integration Tests', async (t) => {
7+
// Default local dev credentials from Supabase CLI
8+
const SUPABASE_URL = 'http://127.0.0.1:54321'
9+
const ANON_KEY =
10+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0'
11+
12+
const supabase = createClient(SUPABASE_URL, ANON_KEY, {
13+
realtime: { heartbeatIntervalMs: 500 },
14+
})
15+
16+
await t.step('should connect to Supabase instance', () => {
17+
assertExists(supabase)
18+
assertEquals(supabase instanceof SupabaseClient, true)
19+
})
20+
21+
await t.step('PostgREST - should query data from public schema', async () => {
22+
const { data, error } = await supabase.from('todos').select('*').limit(5)
23+
24+
// The default schema includes a 'todos' table, but it might be empty
25+
assertEquals(error, null)
26+
assertEquals(Array.isArray(data), true)
27+
})
28+
29+
await t.step('PostgREST - should create and delete a todo', async () => {
30+
// Create a new todo
31+
const { data: createdTodo, error: createError } = await supabase
32+
.from('todos')
33+
.insert({ task: 'Integration Test Todo', is_complete: false })
34+
.select()
35+
.single()
36+
37+
assertEquals(createError, null)
38+
assertExists(createdTodo)
39+
assertEquals(createdTodo!.task, 'Integration Test Todo')
40+
assertEquals(createdTodo!.is_complete, false)
41+
42+
// Delete the created todo
43+
const { error: deleteError } = await supabase.from('todos').delete().eq('id', createdTodo!.id)
44+
assertEquals(deleteError, null)
45+
46+
// Verify the todo was deleted
47+
const { data: fetchedTodo, error: fetchError } = await supabase
48+
.from('todos')
49+
.select('*')
50+
.eq('id', createdTodo!.id)
51+
.single()
52+
53+
assertExists(fetchError)
54+
assertEquals(fetchedTodo, null)
55+
})
56+
57+
await t.step('Authentication - should sign up a user', async () => {
58+
const email = `test-${Date.now()}@example.com`
59+
const password = 'password123'
60+
61+
const { data, error } = await supabase.auth.signUp({
62+
email,
63+
password,
64+
})
65+
66+
assertEquals(error, null)
67+
assertExists(data.user)
68+
assertEquals(data.user!.email, email)
69+
70+
// Clean up by signing out the user
71+
await supabase.auth.signOut()
72+
})
73+
74+
await t.step('Realtime - is able to connect and broadcast', async () => {
75+
const channelName = `channel-${crypto.randomUUID()}`
76+
let channel: RealtimeChannel
77+
const email = `test-${Date.now()}@example.com`
78+
const password = 'password123'
79+
80+
await supabase.auth.signUp({ email, password })
81+
const config = { broadcast: { self: true }, private: true }
82+
channel = supabase.channel(channelName, { config })
83+
await supabase.realtime.setAuth()
84+
85+
const testMessage = { message: 'test' }
86+
let receivedMessage: any
87+
let subscribed = false
88+
let attempts = 0
89+
90+
channel
91+
.on('broadcast', { event: '*' }, (payload) => (receivedMessage = payload))
92+
.subscribe((status) => {
93+
if (status == 'SUBSCRIBED') subscribed = true
94+
})
95+
96+
// Wait for subscription
97+
while (!subscribed) {
98+
if (attempts > 50) throw new Error('Timeout waiting for subscription')
99+
await new Promise((resolve) => setTimeout(resolve, 100))
100+
attempts++
101+
}
102+
103+
attempts = 0
104+
105+
channel.send({ type: 'broadcast', event: 'test-event', payload: testMessage })
106+
107+
// Wait on message
108+
while (!receivedMessage) {
109+
if (attempts > 50) throw new Error('Timeout waiting for message')
110+
await new Promise((resolve) => setTimeout(resolve, 100))
111+
attempts++
112+
}
113+
114+
assertExists(receivedMessage)
115+
assertEquals(supabase.realtime.getChannels().length, 1)
116+
117+
await supabase.removeAllChannels()
118+
})
119+
})

test/deno/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@supabase/supabase-js-deno-tests",
3+
"version": "0.0.1",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"test": "deno task test"
8+
},
9+
"dependencies": {
10+
"@supabase/supabase-js": "workspace:*"
11+
}
12+
}

0 commit comments

Comments
 (0)