TheLuckyLoki
Errors with mocking & testing in supabase edge function running on deno.
Hi everyone - I am sincerely hoping you can save me from pulling out all my hair with this.
At my company we're using supabase, and want to use edge-functions, setting up this part was relatively quick and easy; however, I'm a test-fanatic, and I believe that nothing in our system should go without testing.
Meaning, every edge function should have a decent test-coverage, this INCLUDES mocks/spies/stubs/fakes. The way supabase presents their testing docs, they assume we're always running on database, querying directly on a DB. However, good practices is to mock out our calls and return dummy data to assert whether the system can be proven to be robust in different situations.
However, the native denoland test/mock setup: https://docs.deno.com/runtime/manual/basics/testing/mocking
Does not allow me to effectively spy on or mock the supabase package.
Example of how we setup our client:
import {createClient} from 'https://esm.sh/@supabase/supabase-js';
const default supabase = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_ANON_KEY') ?? '',
);
we then call "supabase.from('some-table).select();" and expect to get some info back.
I'd love to be able to do something like:
const fromStub = stub(supabase, "from", [{'some object data here'}]));
and see the stubbed data returned.
Sadly though, I can confirm that the stub/spy is never called within the production.
I have spent a good 10 hours researching this subject, and have tried many approaches including import_maps and third party libs like sinonJS.
https://stackoverflow.com/questions/69677679/deno-mock-out-named-import-in-unit-test
This is incredibly frustrating, and if any of you have tried this use-case or similar, I'd appreciate some input.
The current documentation at supabase is severely lacking on the subject of mocking/spying/stubbing and it appears that something in the supabase setup makes it not work directly as presented in the deno docs.
5 replies