firebase.config() is empty during integration tests

Viewed 435

I'm writing tests for my Firebase Cloud Functions using Online mode (recommended). I would like my tested code to get actual values from functions.config(), not the mocked ones. Here is my code:

import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
import * as testFn from "firebase-functions-test";

let firebaseTest: any;

const projectConfig = {
  databaseURL: "https://hidden.firebaseio.com",
  storageBucket: "hidden.appspot.com",
  projectId: "hidden",
};

beforeAll((done) => {
  firebaseTest = testFn(projectConfig, "./hidden-adminsdk.json");
  admin.initializeApp()
  done();
});

afterAll(async () => {
  firebaseTest.cleanup();
  await admin.app().delete();
});

test("firebase.config().stripe.secret_key should return actual key", async () => {
  expect(functions.config()).toBeDefined();
  expect(functions.config().stripe).toBeDefined();
});

And the output is:

    expect(received).toBeDefined()

    Received: undefined

      24 | test("firebase.config().stripe.secret_key should return actual key", async () => {
      25 |   expect(functions.config()).toBeDefined();
    > 26 |   expect(functions.config().stripe).toBeDefined();
         |                                     ^
      27 | });

  console.warn node_modules/firebase-functions/lib/logger.js:12
    {"severity":"WARNING","message":"Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail"}

Using the same environment, those config values are present when I run my app inside cloud functions or use Firebase CLI. I don't want to mock my functions.config() using this method I want to write integration tests on testing environment.

1 Answers

I know I am a bit late to the party, but here is the solution:

firebase functions:config:get > .runtimeconfig.json 

And below few links with descriptions on what is what and why:

Apparently during tests firebase is not reaching out to the functions themselves. So there has to be a local instance. If You would use the name provided above - it will be automatically added to the tests. And if You will add this to the gitignore - You will not have this stored in repo ;)

Related