Home > OS >  How to unit test onCall firebase cloud functions?
How to unit test onCall firebase cloud functions?

Time:05-22

I am trying to write my first unit test for a firebase cloud function, specifically the https.onCall function, which is used for the firebase mobile SDKs. So I tried writing it by using examples online and came to this result code:

import "jest";
import helloWorld from "../src/functions/helloworld";
import Message from "../src/types/message";

describe("helloworld", () => {
  test("it should run", () => {
    const req = { body: { data: {} } };
    const res = {
      on: (response: Message) => {
        expect(response).toBe({ text: "Hello from Firebase!", code: 200 });
      },
    };

    helloWorld(req as any, res as any);
  });
});

Just in case I did something wrong in the function itself, I will share the code of the function here:

import { logger, region, https } from "firebase-functions/v1";
import Message from "../types/message";

const helloWorldHandler = region("europe-west1").https.onCall((_, context) => {
  if (context.app == undefined) {
    throw new https.HttpsError("failed-precondition", "The function must be called from an App Check verified app.");
  }

  logger.info("Hello logs!", { structuredData: true });
  const message: Message = {
    text: "Hello from Firebase!",
    code: 200,
  };
  return message;
});

export default helloWorldHandler;

So the test itself runs but I run into the issue, that the result is different from what I am expecting with toBe

I receive the following error:

matcherResult: {
    actual: 'finish',
    expected: { text: 'Hello from Firebase!', code: 200 },
    message: '\x1B[2mexpect(\x1B[22m\x1B[31mreceived\x1B[39m\x1B[2m).\x1B[22mtoBe\x1B[2m(\x1B[22m\x1B[32mexpected\x1B[39m\x1B[2m) // Object.is equality\x1B[22m\n'  
      '\n'  
      'Expected: \x1B[32m{"code": 200, "text": "Hello from Firebase!"}\x1B[39m\n'  
      'Received: \x1B[31m"finish"\x1B[39m',
    name: 'toBe',
    pass: false
  }

I guess the on function on res is wrong and it should be different. Still, the only example firebase provides in their documentation is with a function that uses redirect, so I guess I need another keyword since on is not the right one. I also tried using the send keyword, but it did not work for me. Is there any specific function I need to add to the res for the OnCall to work, or do I have another general issue?

CodePudding user response:

The code above in general was not correct. With this code below my unit test works. Going to change the question name so others can find this easier:

import * as functions from "firebase-functions-test";
import helloWorldHandler from "../src/functions/helloworld";
import Message from "../src/types/message";

describe('Testing "helloWorld"', () => {
  it("test helloWorld if function works", async () => {
    const test = functions();
    const data = {};
    const context = {
      app: {},
    };
    const result: Message = await test.wrap(helloWorldHandler)(data, context);
    expect(result.code).toBe(200);
    expect(result.text).toBe("Hello from Firebase!");
  });
});
  • Related