Testing callable cloud functions with the Firebase CLI shell

Testing callable cloud functions with the Firebase CLI shell

firebase functions:shell
debug firebase functions locally
firebase test callable functions
firebase callable functions
testing firebase http functions
firestore local emulator
the cloud firestore emulator is not running, so calls to firestore will affect production.
usefunctionsemulator

I have been trying new firebase callable cloud functions in firebase functions:shell I keep on getting following error

Request has incorrect Content-Type.

and

RESPONSE RECEIVED FROM FUNCTION: 400, {"error":{"status":"INVALID_ARGUMENT","message":"Bad Request"}}

Here is ho w I am trying to call this function on shell

myFunc.post(dataObject)

I have also tried this

myFunc.post().form(dataObject)

But then I get wrong encoding(form) error. dataObject is valid JSON.

Update:

I figured that I need to use firebase serve for local emulation of these callable https functions. Data needs to be passed in post request like this(notice how its nested in data parameter)

{
 "data":{
    "applicantId": "XycWNYxqGOhL94ocBl9eWQ6wxHn2",
    "openingId": "-L8kYvb_jza8bPNVENRN"
 }
}

What I can't figure still is how do I pass dummy auth info while calling that function via a REST client


I managed to get it working running this from within the functions shell:

myFunc.post('').json({"message": "Hello!"})

Run functions locally, The Firebase CLI includes a Cloud Functions emulator which can emulate the following function types: HTTPS functions; Callable functions; Cloud Firestore functions. You can run functions locally to test them before deploying to production. This applies whether you're using the functions shell or firebase  And lastly, if you're like me, and you prefer to use a programmer's editor such as VS Code to write your all JavaScript, you can easily emulate functions by sending code you want to run to the Firebase CLI. This command will run test code from a file redirected through standard input: $ firebase experimental:functions:shell . tests.js Happy testing!


As far as I can tell, the second parameter to the function contains all of the additional data. Pass an object that contains a headers map and you should be able to specify anything you want.

myFunc.post("", { headers: { "authorization": "Bearer ..." } });

If you're using Express to handle routing, then it just looks like:

myApp.post("/my-endpoint", { headers: { "authorization": "Bearer ..." } });

Testing callable cloud functions with the Firebase CLI shell, I managed to get it working running this from within the functions shell: myFunc.​post('').json({"message": "Hello!"}). The Cloud Functions shell provides an interactive shell for invoking functions with test data. The shell supports all trigger types. Set up admin credentials (optional) If you want your functions tests to interact with Google APIs or other Firebase APIs via the Firebase Admin SDK, you may need to set up admin credentials.


If you take a look at the source code, you can see that it is just a normal https post function With an authentication header that contains a json web token, I'd recommend using the unit test api for https functions and mocking the header methods to return a token from a test user as well as the request body

[Update] Example

const firebase = require("firebase");
var config = {
  your config
};
firebase.initializeApp(config);
const test = require("firebase-functions-test")(
  {
    your config
  },
  "your access token"
);
const admin = require("firebase-admin");
const chai = require("chai");
const sinon = require("sinon");

const email = "test@test.test";
const password = "password";
let myFunctions = require("your function file");
firebase
  .auth()
  .signInWithEmailAndPassword(email, password)
  .then(user => user.getIdToken())
  .then(token => {
    const req = {
      body: { data: { your:"data"} },
      method: "POST",
      contentType: "application/json",
      header: name =>
        name === "Authorization"
          ? `Bearer ${token}`
          : name === "Content-Type" ? "application/json" : null,
      headers: { origin: "" }
    };
    const res = {
      status: status => {
        console.log("Status: ", status);
        return {
          send: result => {
            console.log("result", result);
          }
        };
      },
      getHeader: () => {},
      setHeader: () => {}
    };
    myFunctions.yourFunction(req, res);
  })
  .catch(console.error);

Testing callable cloud functions with the Firebase CLI shell, I have been trying new firebase callable cloud functions in firebase functions:​shell I keep on getting following error Request has incorrect Content-Type. and  Start testing with the gcloud CLI Firebase Test Lab provides cloud-based infrastructure for testing Android apps, including full integration with the gcloud command-line interface (CLI). This


Testing firebase cloud functions locally using cloud functions shell, Also, it would also be easy to debug your code using console statements if we can run the functions locally. As of now, there are three tools namely firebase serve,  The Firebase CLI includes a Cloud Functions emulator which can emulate the following function types: HTTPS functions; Callable functions; Cloud Firestore functions; You can run functions locally to test them before deploying to production. Note: Code changes you make during an active session are automatically reloaded by the emulator. If your code needs to be transpiled (TypeScript, React) make sure to do so before running the emulator.


Testing Functions Locally with the Cloud , If you want to try out your code before you deploy it to Cloud Functions, you can do that with the Cloud Functions shell in the Firebase CLI  The Cloud Functions for Firebase client SDKs let you call functions directly from a Firebase app. To call a function from your app in this way, write and deploy an HTTPS Callable function in Cloud Functions, and then add client logic to call the function from your app. It's important to keep in mind that HTTPS callable functions are similar but


Testing database functions locally · Issue #4 · firebase/firebase , It looks like firebase cli tool bundles all dependencies and upload it and I run the function locally using Cloud Functions shell according to this link yet support local testing of HTTP callable functions, but that's coming soon  Test with the Firebase console; Test with the gcloud CLI; Test with CI systems; Test with Android Studio; Analyze test results; Test on available devices; Run an instrumentation test; Run a Robo test; Run a Game Loop test; Take screenshots in a test; Test with virtual devices; Extend with Cloud Functions; REST API reference