Search code examples
firebaseunit-testingtestinggoogle-cloud-functions

Firebase Functions Test fails to wrap v2 function


I have 2 Firebase functions, hello in v1 and hello2 in v2

import { https } from "firebase-functions";
import { onCall } from "firebase-functions/v2/https";

export const hello = https.onCall((request) => {
  return `Hello ${request.data.text}`;
});

export const hello2 = onCall((request) => {
  return `Hello ${request.data.text}`;
});

hello can be wrapped with firebase-functions-test SDK with no issue, but hello2 throws an error. Is firebase-functions-test supports only v1 but not v2? How can I do this for v2 functions?

import { hello, hello2 } from ".";
import { expect } from "chai";
import { wrap } from "firebase-functions-test/lib/main";

describe("hello function", () => {
  it("returns expected output", async () => {
    // Create a test request object.
    const request = {
      body: {
        text: "World",
      },
    };

    const wrapped = wrap(hello);
    // The following line fails with No overload matches this call.
    const wrapped2 = wrap(hello2);

    const response = await wrapped(request);
    // The following line fails with Argument of type '{ body: { text: string; }; }' is not assignable to parameter of type 'CallableRequest<any>'.
    const response2 = await wrapped2(request);

    expect(response).equal("Hello World");
    expect(response2).equal("Hello World");
  });
});

Solution

  • Answering as a Community Wiki, so that it may help others too.

    As @samthecodingman mentioned in the comment, According to firebase/firebase-functions-test Issue #163, as of now v2 onCall functions are not yet supported.