Our great sponsors
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
Backend development requires robust and reliable tools in today's rapidly evolving technology landscape. While Firebase has been a go-to choice for many developers, there is now an open-source alternative called Supabase that offers a PostgreSQL-based backend. Supabase combines the robustness of PostgreSQL with the ease of use and scalability of Firebase, providing a compelling solution for backend development. One crucial aspect of utilizing Supabase is testing its Edge Functions, which play a vital role in extending the functionality of your application while ensuring its reliability. In this blog post, we will explore the process of setting up Supabase, delve into the world of Supabase Edge Functions, and learn how to write and test a simple hello-world function and run Edge Functions locally for efficient development.
// deno-test.ts // Import necessary libraries and modules import { assert, assertExists, assertEquals, } from "https://deno.land/[email protected]/testing/asserts.ts"; import { createClient, SupabaseClient, } from "https://esm.sh/@supabase/[email protected]"; import { delay } from 'https://deno.land/x/[email protected]/mod.ts'; // Setup the Supabase client configuration const supabaseUrl = Deno.env.get("SUPABASE_URL") ?? ""; const supabaseKey = Deno.env.get("SUPABASE_ANON_KEY") ?? ""; const options = { auth: { autoRefreshToken: false, persistSession: false, detectSessionInUrl: false } }; // Test the creation and functionality of the Supabase client const testClientCreation = async () => { var client: SupabaseClient = createClient(supabaseUrl, supabaseKey, options); // Check if the Supabase URL and key are provided if (!supabaseUrl) throw new Error('supabaseUrl is required.') if (!supabaseKey) throw new Error('supabaseKey is required.') // Test a simple query to the database const { data: table_data, error: table_error } = await client.from('my_table').select('*').limit(1); if (table_error) { throw new Error('Invalid Supabase client: ' + table_error.message); } assert(table_data, "Data should be returned from the query."); }; // Test the 'hello-world' function const testHelloWorld = async () => { var client: SupabaseClient = createClient(supabaseUrl, supabaseKey, options); // Invoke the 'hello-world' function with a parameter const { data: func_data, error: func_error } = await client.functions.invoke('hello-world', { body: { name: 'bar' } }); // Check for errors from the function invocation if (func_error) { throw new Error('Invalid response: ' + func_error.message); } // Log the response from the function console.log(JSON.stringify(func_data, null, 2)); // Assert that the function returned the expected result assertEquals(func_data.message, 'Hello bar!'); }; // Register and run the tests Deno.test("Client Creation Test", testClientCreation); Deno.test("Hello-world Function Test", testHelloWorld);