3

I am trying to set up unit testing for my firebase cloud-function. but getting this error

Error: Wrap function is only available for onCall HTTP functions, not onRequest.

this is how my app.test.ts looks like

/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-unused-vars */
import {stub} from "sinon";
import * as admin from "firebase-admin";
import funcTest from "firebase-functions-test";
admin.initializeApp();

describe("Index Test", function() {
  let adminInitStub:any;
  let appCloudFunction:any;
  const test = funcTest();
  before(async () => {
    adminInitStub = stub(admin, "initializeApp");
    appCloudFunction = await import("../index");
  });

  after(() => {
    adminInitStub.restore();
    test.cleanup();
  });
  it("should always pass", function() {
    const wrapped = test.wrap(appCloudFunction.app);
    wrapped(null);
  });
});

and my cloud function(index.ts) looks something like this

import * as functions from "firebase-functions";
import app from "./app/app";
exports.app = functions.https.onRequest(app);

here app is an simple express application.

Can anyone help me solve this problem?

1
  • 1
    I think the error message is pretty clear. What you're trying to do is not supported. If you have a feature request to add support for onRequest, you should file an issue with Firebase directly. Stack Overflow can't help you. Commented Dec 21, 2021 at 23:39

1 Answer 1

3

The wrap() function from the unit testing package is used to reformat the data you pass into it in a way that can be understood by the Background Event Cloud Functions (these have a handler signature that looks like (data, context) => {} or (context) => {}) when it gets processed. You can see what the function is doing here.

A "raw" HTTPS Request function has a handler signature that looks like (request, response) => {}. Because there is no fancy processing going on here, you can just call the function directly passing in your own Request and Response objects directly. The documentation has further details on this.

// index.js
export myTestFunction = functions.https.onRequest(myExpressApp);
// index.test.js
import { myTestFunction } from "./index.js"

const req = {
  url: "https://example.com/path/to/test",
  query: {
    "id": "10"
  }
}

const res = {
  status: () => {} // your test shim
  json: () => {} // your test shim
}

myTestFunction(req, res);

// wait for res to be processed
// do tests

You may run into issues passing in such a simple response object this way because you are using Express behind the Cloud Function and it does a lot of internal function calling (e.g. res.json would call res.send, res.status() needs to return itself for chaining). For that situation you are better off mocking Request and Response objects using an express unit testing library and then passing them into your function to be tested.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.