Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative tests for edge functions with Deno #65

Open
2 tasks done
hammerlscs opened this issue Aug 24, 2023 · 4 comments
Open
2 tasks done

Negative tests for edge functions with Deno #65

hammerlscs opened this issue Aug 24, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@hammerlscs
Copy link

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

I am trying to write some negative tests for my edge functions, for example:

const testSomeFunction = async () => {
  const { data, error } = await supabaseUser.functions.invoke('some-function', {
    body: {
      userId: 'invalid-id'
    }
  })

  assertEquals(error.message, 'Edge Function returned a non-2xx status code')
  assertEquals(data, null)
}

But when running deno test, the test fails with:

error: Leaking resources:
  - A fetch response body (rid 18) was created during the test, but not consumed during the test. Consume or close the response body `ReadableStream`, e.g `await resp.text()` or `await resp.body.cancel()`.

But I cannot access the response to consume/close/cancel/... it.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Create edge function
  2. Create negative test for edge function
  3. Run deno test

Expected behavior

I expect the test to pass, without any leaking resources.

System information

  • OS: Windows 11
  • Version of supabase-js: 2.26.0
  • Version of Node.js: 18.15.0
  • Version of Deno: 1.36.2
@LaskeJu
Copy link

LaskeJu commented Nov 20, 2023

Ugly workaround to make my tests pass ...

Deno.test("Test something", async () => {
  try {
    await testSomething();
  } catch (error) {
    console.error(error);
  } finally {
    const resources = Deno.resources();
    for (const rid in resources) {
      if (resources[rid] === "fetchResponse") {
        Deno.close(parseInt(rid));
      }
    }
  }
});}

@carlosdp
Copy link

carlosdp commented Feb 5, 2024

Also running into this when running tests with supabase-js, it seems to only happen on tests that are testing for an error response

@chrissy0
Copy link

chrissy0 commented Mar 7, 2024

Any news on this?

@hammerlscs
Copy link
Author

hammerlscs commented Mar 7, 2024

This worked for us:

const { data, error } = await supabaseUser.functions.invoke('some-function', {
  body: {
    userId: 'invalid-id'
  }
})
assertEquals(data, null)
await assertError(error, 'expected error message')

And:

export async function assertError(
  error: FunctionsHttpError | FunctionsRelayError | FunctionsFetchError,
  expectedErrorMessage: string,
) {
  assertEquals(error.name, 'FunctionsHttpError')
  const { message: actualErrorMessage }: { message: string } = await error.context.json()
  assertEquals(
    actualErrorMessage,
    expectedErrorMessage,
  )
}

Because we always return a message with an error response:

const responseData = { message, data }
return new Response(JSON.stringify(responseData), {
  headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  status: httpStatusCode,
})

Which we can assert in the edge function test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants