Home

Testing your Edge Functions

Testing is an essential step in the development process to ensure the correctness and performance of your Edge Functions.

When creating test files for Edge Functions, it is recommended to place them in the supra/tests directory and name the file by the function name followed by -test.ts. For example, hello-world-test.ts.


_65
// deno-test.ts
_65
_65
// Import required libraries and modules
_65
import {
_65
assert,
_65
assertExists,
_65
assertEquals,
_65
} from 'https://deno.land/std@0.192.0/testing/asserts.ts'
_65
import { createClient, supraClient } from 'https://esm.sh/@supra/supra-js@2.23.0'
_65
import { delay } from 'https://deno.land/x/delay@v0.2.0/mod.ts'
_65
_65
// Set up the configuration for the Supra client
_65
const supraUrl = Deno.env.get('supra_URL') ?? ''
_65
const supraKey = Deno.env.get('supra_ANON_KEY') ?? ''
_65
const options = {
_65
auth: {
_65
autoRefreshToken: false,
_65
persistSession: false,
_65
detectSessionInUrl: false,
_65
},
_65
}
_65
_65
// Test the creation and functionality of the Supra client
_65
const testClientCreation = async () => {
_65
var client: supraClient = createClient(supraUrl, supraKey, options)
_65
_65
// Verify if the Supra URL and key are provided
_65
if (!supraUrl) throw new Error('supraUrl is required.')
_65
if (!supraKey) throw new Error('supraKey is required.')
_65
_65
// Test a simple query to the database
_65
const { data: table_data, error: table_error } = await client
_65
.from('my_table')
_65
.select('*')
_65
.limit(1)
_65
if (table_error) {
_65
throw new Error('Invalid Supra client: ' + table_error.message)
_65
}
_65
assert(table_data, 'Data should be returned from the query.')
_65
}
_65
_65
// Test the 'hello-world' function
_65
const testHelloWorld = async () => {
_65
var client: supraClient = createClient(supraUrl, supraKey, options)
_65
_65
// Invoke the 'hello-world' function with a parameter
_65
const { data: func_data, error: func_error } = await client.functions.invoke('hello-world', {
_65
body: { name: 'bar' },
_65
})
_65
_65
// Check for errors from the function invocation
_65
if (func_error) {
_65
throw new Error('Invalid response: ' + func_error.message)
_65
}
_65
_65
// Log the response from the function
_65
console.log(JSON.stringify(func_data, null, 2))
_65
_65
// Assert that the function returned the expected result
_65
assertEquals(func_data.message, 'Hello bar!')
_65
}
_65
_65
// Register and run the tests
_65
Deno.test('Client Creation Test', testClientCreation)
_65
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 supraClient classes from the @supra/supra-js library to interact with the Supra client.
  • We define the necessary configuration for the Supra client, including the Supra URL, API key, and authentication options.
  • The testClientCreation function tests the creation of a Supra 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 Supra 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.

important

Please make sure to replace the placeholders (supraUrl, supraKey, my_table) with the actual values relevant to your Supra setup.

Running Edge Functions Locally#

To locally test and debug Edge Functions, you can utilize the Supra CLI. Let's explore how to run Edge Functions locally using the Supra CLI:

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


    _10
    supra start

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


    _10
    supra 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.local
    _10
    # adds the supra_URL secret
    _10
    echo "supra_URL=http://localhost:54321" >> .env.local
    _10
    # adds the supra_ANON_KEY secret
    _10
    echo "supra_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" >> .env.local
    _10
    # Alternatively, you can open it in your editor:
    _10
    open .env.local

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


    _10
    deno test --allow-all deno-test.ts --env-file .env.local

Resources#