Testing your Edge Functions
Writing Unit Tests for Edge Functions using Deno Test
Testing is an essential step in the development process to ensure the correctness and performance of your Edge Functions.
Testing in Deno
Deno has a built-in test runner that you can use for testing JavaScript or TypeScript code. You can read the official documentation for more information and details about the available testing functions.
Folder structure
We recommend creating your testing in a supabase/tests
directory, using the same name as the Function followed by -test.ts
:
_10└── supabase_10 ├── functions_10 │ ├── function-one_10 │ │ └── index.ts_10 │ └── function-two_10 │ │ └── index.ts_10 │ └── tests_10 │ └── function-one-test.ts # Tests for function-one_10 │ └── function-two-test.ts # Tests for function-two_10 └── config.toml
Example script
The following script is a good example to get started with testing your Edge Functions:
This test case consists of two parts. The first part tests the client library and verifies that the database can be connected to and returns values from a table (my_table
). The second part tests the edge function and checks if the received value matches the expected value. Here's a brief overview of the code:
- We import various testing functions from the Deno standard library, including
assert
,assertExists
, andassertEquals
. - We import the
createClient
andSupabaseClient
classes from the@supabase/supabase-js
library to interact with the Supabase client. - We define the necessary configuration for the Supabase client, including the Supabase URL, API key, and authentication options.
- The
testClientCreation
function tests the creation of a Supabase client instance and queries the database for data from a table. It verifies that data is returned from the query. - The
testHelloWorld
function tests the "Hello-world" Edge Function by invoking it using the Supabase client'sfunctions.invoke
method. It checks if the response message matches the expected greeting. - We run the tests using the
Deno.test
function, providing a descriptive name for each test case and the corresponding test function.
Please make sure to replace the placeholders (supabaseUrl
, supabaseKey
, my_table
) with the actual values relevant to your Supabase setup.
Running Edge Functions locally
To locally test and debug Edge Functions, you can utilize the Supabase CLI. Let's explore how to run Edge Functions locally using the Supabase CLI:
-
Ensure that the Supabase server is running by executing the following command:
_10supabase start -
In your terminal, use the following command to serve the Edge Functions locally:
_10supabase functions serveThis command starts a local server that runs your Edge Functions, enabling you to test and debug them in a development environment.
-
Create the environment variables file:
_10# creates the file_10touch .env.local_10# adds the SUPABASE_URL secret_10echo "SUPABASE_URL=http://localhost:54321" >> .env.local_10# adds the SUPABASE_ANON_KEY secret_10echo "SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" >> .env.local_10# Alternatively, you can open it in your editor:_10open .env.local -
To run the tests, use the following command in your terminal:
_10deno test --allow-all supabase/functions/function-one-test.ts --env-file .env.local
Resources
- Full guide on Testing Supabase Edge Functions on Mansueli's tips