title: Unit testing of HttpsCallable cloud functions - online mode
body: <p>I am designing a backend api (for android and iOS apps) around <a href="https://firebase.google.com/docs/functions/callable" rel="nofollow noreferrer">HttpsCallable</a> cloud functions. It becomes quite cumbersome to test them through the app, so I wish to switch to unit testing the functions (before production deployment) using the <code>firebase-functions-test</code> tool. I have been following this <a href="https://firebase.google.com/docs/functions/unit-testing" rel="nofollow noreferrer">unit testing tutorial</a>. </p>
  
  <p>I am having some issues running the unit tests in online mode. Let me provide some details.  </p>
  
  <p>Here is my `package.json' content:</p>
  
  <pre><code>{
    "name": "functions",
    "description": "Cloud Functions for Firebase",
    "scripts": {
      "lint": "eslint .",
      "serve": "firebase serve --only functions",
      "shell": "firebase functions:shell",
      "start": "npm run shell",
      "deploy": "firebase deploy --only functions",
      "logs": "firebase functions:log",
      "test": "mocha --reporter spec"
    },
    "engines": {
      "node": "8"
    },
    "dependencies": {
      "@google-cloud/tasks": "^1.9.0",
      "@googlemaps/google-maps-services-js": "^2.0.2",
      "chai": "^4.2.0",
      "firebase-admin": "^8.6.0",
      "firebase-functions": "^3.3.0",
    },
    "devDependencies": {
      "eslint": "^5.12.0",
      "eslint-plugin-promise": "^4.0.1",
      "firebase-functions-test": "^0.1.7",
      "mocha": "^7.1.1"
    },
    "private": true
  }
  </code></pre>
  
  <p>I am using google APIs (<em>Directions</em>, <em>Geocoding</em> etc) from firebase backend, therefore to be able to access them while running my tests, I configured my tests located at <code>test/index.test.js</code> as recommended in the <em>Unit testing tutorial</em> as follows:</p>
  
  <pre><code>const firebaseConfig = {
    apiKey: ...,
    authDomain: ...,
    databaseURL: "https://my-project.firebaseio.com",
    projectId: "my-project",
    storageBucket: "my-project.appspot.com",
    messagingSenderId: ********,
    appId: *********
  };
  
  
  const test = require('firebase-functions-test')(firebaseConfig
    , 'path_to_json_keyfile/myproject.json');
  </code></pre>
  
  <p>Below is the sample test code. Note that all my callable functions return <a href="https://firebase.google.com/docs/reference/node/firebase.functions.HttpsError" rel="nofollow noreferrer">HttpsError</a>, and the test below, simply checks for the <code>code</code> field of <code>HttpsError</code>, if its <code>ok</code>, the test passes. The functions I am testing reside in a separate <code>rides.js</code> file, which the <code>index.js</code> exports as <code>exports.rides = require(./rides.js)</code> (not shown below)</p>
  
  <pre><code>const chai = require('chai');
  const assert = chai.assert;
  
  describe('Cloud functions', () =&gt; {
    let rideFunctions;
    before(() =&gt; {
      rideFunctions = require('../index.js');
    });
  
    after(() =&gt; {
      test.cleanup();
    });
  
    describe('getRideEstimate', () =&gt; {
  
      it('should return ok', async () =&gt; {
        data = {
          riderType: 1,
          pickup_lat: 37.0,
          pickup_lng: -122,
          dropoff_lat: 37.01,
          dropoff_lng: -122.01,
          scheduledTime: 1585939000,
          ts: 1585939000,
          tz_id: "uslax"
        }
  
        context = {
          auth: {
            uid: AUTH_ID_FOR_USER_ACCOUNT
          }
        };
  
        const wrappedGetRideEstimate = test.wrap(rideFunctions.rides.getRideEstimate);
  
        let httpsError = await wrappedGetRideEstimate(data, context);
        return assert.equal(httpsError.code, 'ok');
      });
  
  
    })
  
    describe('testCallable', () =&gt; {
      it('should return ok', async () =&gt; {
        data = {}
        context = {}
        const wrappedTestCallable = test.wrap(rideFunctions.rides.testCallable);
        let httpsError = await wrappedTestCallable(data, context);
        return assert.equal(httpsError.code, 'ok');
  
      })
    })
  
  
  })
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <ol>
  <li>My simple <code>testCallable</code> function of the form </li>
  </ol>
  
  <pre><code>exports.testCallable = functions.https.onCall((data, context) =&gt; {
      console.log('testCallable');
      return new functions.https.HttpsError('ok', '', '');
  })
  </code></pre>
  
  <p>passes the test (as expected) but inexplicably, it seems it is running in the offline mode, as there is no record of it on cloud function logs in Firebase Console. Also, if I disable connectivity of my laptop, the test result remains the same suggesting that somehow this function is running in the offline mode. </p>
  
  <ol start="2">
  <li><p>My <code>getRideEstimate</code> function which calls the google <code>Directions</code> API, returns a lengthy <code>400</code> error indicating some issue with forming the request to Directions API. I don't know if this error is related to the first problem, but since the Directions API call is embedded deeper inside the <code>getRideEstimate</code> function, it does suggest that the function is getting called, but somehow the API call is failing. </p></li>
  <li><p>Is there any other way to test HttpsCallable functions in online mode?</p></li>
  </ol>