Edge Functions - inconsistent results when running .invoke locally using supabase serve
I have this simple edge function, im trying to test requests that are not POST. I keep getting mixed responses. Please help 🙏
message: Method not allowed
status: 405
headers: POST
or
message: undefined
status: 502
headers: null
Edge Function
Test
47 Replies
I don't see you handling the OPTIONS request.
When you use .invoke() from a browser you have to handle a return for the OPTIONS request.
CORS (Cross-Origin Resource Sharing) support for Invoking from the ...
Add CORS headers to invoke Edge Functions from the browser.
im using invoke in Deno.test not from a browser?
No idea how that works, but assume it would not do options request.
What do you see in the EDGE function logs for invocation in both cases and the console.log of the type of request?
on a successful run i get this
2025-07-03T18:06:45.639379846Z Serving functions on http://127.0.0.1:54321/functions/v1/<function-name>
2025-07-03T18:06:45.639394387Z - http://127.0.0.1:54321/functions/v1/contact-us
2025-07-03T18:06:45.639395846Z - http://127.0.0.1:54321/functions/v1/current-usage
2025-07-03T18:06:45.639396554Z - http://127.0.0.1:54321/functions/v1/delete-account
2025-07-03T18:06:45.639397221Z - http://127.0.0.1:54321/functions/v1/update-system-settings-handler
2025-07-03T18:06:45.639397887Z Using supabase-edge-runtime-1.67.4 (compatible with Deno v1.45.2)
2025-07-03T18:06:46.554569096Z serving the request with supabase/functions/update-system-settings-handler
2025-07-03T18:06:46.750338721Z [Info] Request: {}
2025-07-03T18:06:46.750369013Z
2025-07-03T18:06:46.750371221Z [Info] Method not allowed: PUT
2025-07-03T18:06:46.750372221Z
on a failed run i get these logs
025-07-03T18:06:51.427318626Z Serving functions on http://127.0.0.1:54321/functions/v1/<function-name>
2025-07-03T18:06:51.427339542Z - http://127.0.0.1:54321/functions/v1/contact-us
2025-07-03T18:06:51.427341042Z - http://127.0.0.1:54321/functions/v1/current-usage
2025-07-03T18:06:51.427341792Z - http://127.0.0.1:54321/functions/v1/delete-account
2025-07-03T18:06:51.427342417Z - http://127.0.0.1:54321/functions/v1/update-system-settings-handler
2025-07-03T18:06:51.427343084Z Using supabase-edge-runtime-1.67.4 (compatible with Deno v1.45.2)
There is no consistency between it running successfully or failing.
It’s like it is not running the code. When it fails, I don’t get the first line of the function, outputting the request - [Info] Request: {}
Anything useful in this log?

I don't recognize your logs. Mine look nothing like that.
But it is an already deployed function.

Are you not running a hosted function?
This is when running the edge functions locally.
Add that to your title or first post.
I don't do that so not sure what type of things you could run into.
Is the deno.test also a locally running thing?
ok no problem sure.
when running against our dev, i didn’t see any errors.

How do you call this function in Deno.serve?
// Pass the handler with the default
tasks
module to Deno.serve
Deno.serve(createHandler());
I completed some more testing this morning, commenting out all other tests, this passes every time as soon as i uncomment like in the example below there is an intermittent failure.
sorry had to split the example into two part due to character limitationOk, test works in deno as well.
I've ran the test multiple times, test passes
Have you tried adding a second test, for example, check that POST is allowed.
I found that as soon as there was a second test in the file it started random errors
I didn't run a test for that, but checked via browser. That works. Let me run a test for that too.
No issues
Function:
there seems to be some sort of clash between this test
and
still same issue?
or some other error
same issue
as soon as i introduce this one line
const handler = createHandler(mockTasks);

And if you only run this one, does that work?
this has been working for a few months now. wednesday, before i made any changes i checked to ensure all the tests and passed and was faced with this issue
for reference, this works

adding the line and it fails

I've updated my test to use mock request as you're doing but I still don't get any errors.
Here is the code, the only difference is I'm not passing in mock tasks.
I've deno 2.4.0 installed.
ive done a little more digging and i think it might be related to the end point not being ready. adding a 1 sec timeout seems to resolve the problem. any thoughts on that?
Vitest has a built in retry mechanism. I don't usually use deno tests but if deno test has this functionality then use that instead of setting a hardcoded timeout
checking the service is available works which is odd

I think this is functioning the same way as setting a timeout, just some extra ms before and that seems to do the job. But i didn't have to do any of this. I ran all tests on Linux. Are you on Linux?
Try adding a retry mechanism
https://docs.deno.com/examples/exponential_backoff/
im running on mac using devcontainers
@inder im still seeing this error, i see a lot of 502 responses from the edge functions but once you broswer to the end point it seems to work.
Function (http://localhost:54321/functions/v1/update-system-settings-handler) reachable: 502
message: undefined
status: 502
headers: null
Retry 1 failed.
Function (http://localhost:54321/functions/v1/update-system-settings-handler) reachable: 401
message: Method not allowed
status: 405
headers: POST
when i run this in my github action, i get the following


This is on deno version 2.4.0?
and the cli version?
this my action
edge-functions-tests:
name: Edge Functions Testing
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Supabase CLI
uses: supabase/setup-cli@v1
with:
version: latest
- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: latest
- name: Pin Postgres version
run: |
mkdir -p supabase/.temp
echo "15.8.1.054" > supabase/.temp/postgres-version
- name: Start Supabase
run: |
supabase start
- name: Serve edge functions
run: |
supabase functions serve \
--env-file=./supabase/functions/.env.test \
--import-map=./deno.json \
| tee supabase-functions.log &
echo "Serving edge functions… pid=$!"
# - name: Wait for edge functions to be ready # run: | # echo "Waiting for Supabase Edge Functions to be ready..." # until grep -q "Using supabase-edge-runtime-" supabase-functions.log; do # sleep 0.5 # done # echo "Supabase Edge Functions are ready!" - name: Wait for edge functions to start run: sleep 10 - name: Run edge-function tests run: deno test --allow-all --env=./.env.test.supabase ./test/update-system-settings-handler-test.ts working-directory: ./supabase/functions this my action
# - name: Wait for edge functions to be ready # run: | # echo "Waiting for Supabase Edge Functions to be ready..." # until grep -q "Using supabase-edge-runtime-" supabase-functions.log; do # sleep 0.5 # done # echo "Supabase Edge Functions are ready!" - name: Wait for edge functions to start run: sleep 10 - name: Run edge-function tests run: deno test --allow-all --env=./.env.test.supabase ./test/update-system-settings-handler-test.ts working-directory: ./supabase/functions this my action
I'll take a look
You're running 2 tests right? POST and NOT POST
yes
this is the test class and function, plus cors
@SB What version of
@trigger.dev/sdk
are you using? When I try to import tasks from sdk I get error."@trigger.dev/sdk": "npm:@trigger.dev/sdk/v3"
Is this the version you are using
Its using 3.3.17

Download https://registry.npmjs.org/@trigger.dev/sdk/-/sdk-3.3.17.tgz
this is the same version as me
But that's what its using
You need to use npm:@trigger.dev/sdk/v3
https://github.com/singh-inder/deno-supa-test/actions/runs/16179861642/job/45673751770
I hardcoded the supabase values that's why I removed the env flag from deno and supabase
I copied exactly as you did and it works. Thanks
I guess my next question is how can you pass environment variables into the setting up of the functions, like I was doing with the serve?
What is the cause of this? Two weeks ago this worked with no issues.
Two weeks ago deno was on v2.3.7, so maybe with v2.4 something was changed. You'll have to go though their changelog
If testing with deno is not a requirement, you can try bun
For envs, I've pushed a new commit. You can do something like this
@inder just for completeness and possibly for anyone else with this issue. i spent the morning stepping through every difference between how you had setup the tests and how i had done it. below is the final yaml that is now working.
one thing i did notice was that without import map in the serve, i seems to get similar errors whereby the end point was reachable with a 401 status but the response when invoked with 503
All my tests remained the same (I remove the retries and anything added from this thread)
one the biggest changes i made was split the database and edge function tests, im still a lost though as to what changed between the above working yaml and the old version which broke (shared below)
Thankyou for posting your solution.