Edge Functions

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/functions/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:

function-one-test.ts

_61
// Import required libraries and modules
_61
import { assert, assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'
_61
import { createClient, SupabaseClient } from 'jsr:@supabase/supabase-js@2'
_61
_61
// Will load the .env file to Deno.env
_61
import 'https://deno.land/x/[email protected]/load.ts'
_61
_61
// Set up the configuration for the Supabase client
_61
const supabaseUrl = Deno.env.get('SUPABASE_URL') ?? ''
_61
const supabaseKey = Deno.env.get('SUPABASE_ANON_KEY') ?? ''
_61
const options = {
_61
auth: {
_61
autoRefreshToken: false,
_61
persistSession: false,
_61
detectSessionInUrl: false,
_61
},
_61
}
_61
_61
// Test the creation and functionality of the Supabase client
_61
const testClientCreation = async () => {
_61
var client: SupabaseClient = createClient(supabaseUrl, supabaseKey, options)
_61
_61
// Verify if the Supabase URL and key are provided
_61
if (!supabaseUrl) throw new Error('supabaseUrl is required.')
_61
if (!supabaseKey) throw new Error('supabaseKey is required.')
_61
_61
// Test a simple query to the database
_61
const { data: table_data, error: table_error } = await client
_61
.from('my_table')
_61
.select('*')
_61
.limit(1)
_61
if (table_error) {
_61
throw new Error('Invalid Supabase client: ' + table_error.message)
_61
}
_61
assert(table_data, 'Data should be returned from the query.')
_61
}
_61
_61
// Test the 'hello-world' function
_61
const testHelloWorld = async () => {
_61
var client: SupabaseClient = createClient(supabaseUrl, supabaseKey, options)
_61
_61
// Invoke the 'hello-world' function with a parameter
_61
const { data: func_data, error: func_error } = await client.functions.invoke('hello-world', {
_61
body: { name: 'bar' },
_61
})
_61
_61
// Check for errors from the function invocation
_61
if (func_error) {
_61
throw new Error('Invalid response: ' + func_error.message)
_61
}
_61
_61
// Log the response from the function
_61
console.log(JSON.stringify(func_data, null, 2))
_61
_61
// Assert that the function returned the expected result
_61
assertEquals(func_data.message, 'Hello bar!')
_61
}
_61
_61
// Register and run the tests
_61
Deno.test('Client Creation Test', testClientCreation)
_61
Deno.test('Hello-world Function Test', testHelloWorld)

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, and assertEquals.
  • We import the createClient and SupabaseClient 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's functions.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.

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:

  1. Ensure that the Supabase server is running by executing the following command:


    _10
    supabase start

  2. In your terminal, use the following command to serve the Edge Functions locally:


    _10
    supabase functions serve

    This command starts a local server that runs your Edge Functions, enabling you to test and debug them in a development environment.

  3. Create the environment variables file:


    _10
    # creates the file
    _10
    touch .env
    _10
    # adds the SUPABASE_URL secret
    _10
    echo "SUPABASE_URL=http://localhost:54321" >> .env
    _10
    # adds the SUPABASE_ANON_KEY secret
    _10
    echo "SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" >> .env
    _10
    # Alternatively, you can open it in your editor:
    _10
    open .env

  4. To run the tests, use the following command in your terminal:


    _10
    deno test --allow-all supabase/functions/tests/function-one-test.ts

Resources