firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

"Can't determine Firebase Database URL." when initialize Admin SDK #171

Open ASE55471 opened 1 year ago

ASE55471 commented 1 year ago

Version info

**firebase-functions-test:"^2.4.0"

**firebase-functions:"^3.23.0"

**firebase-admin:"^11.0.1"

Test case

function code

import { https } from 'firebase-functions';
import { Timestamp } from 'firebase-admin/firestore';
import * as admin from 'firebase-admin';
admin.initializeApp();

let defaultDatabase = admin.database();
let defaultFirestore = admin.firestore();

test code

import { test } from "@jest/globals";
import { functionF} from "../../src/functionF";
import firebaseFunctionsTest from "firebase-functions-test";

const {wrap , firestore} = firebaseFunctionsTest({
    projectId: 'project-id',
    storageBucket: 'project-id.appspot.com',
    databaseURL: "https://project-id-default-rtdb.asia-southeast1.firebasedatabase.app",
}, './serviceAccountkey.json');

describe('test1', () => {
    test('test', () => {
        const wrappedFunctionF = wrap(functionF);
        const snap = firestore.makeDocumentSnapshot({ foo: 'bar' }, 'document/path');
        wrappedFunctionF(snap);
    });
});

I tried fill databaseURL without "-default-rtdb" but still throw same error.

Relative StackOverflow question I asked: https://stackoverflow.com/questions/73657752/how-to-initialize-admin-sdk-with-firebase-functions-test

Expected behavior

Finish the test without error .

Actual behavior

It throw error.

    Can't determine Firebase Database URL.

      4 | admin.initializeApp();
      5 |
    > 6 | let defaultDatabase = admin.database();
        |                             ^
      7 | let defaultFirestore = admin.firestore();
      8 |
josephepia commented 1 year ago

@ASE55471 did you find any solution?

ASE55471 commented 1 year ago

@josephepia No, I still have no ideas on this issue.

josephepia commented 1 year ago

@ASE55471 my workaround was to manually add the database url to the index.ts file and then remove it before submitting a production.

functions/src/index.ts

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

admin.initializeApp({
  databaseURL: "http://127.0.0.1:9000/?ns=nameProject-default-rtdb", //<- add and remove 
});

It can be improved with a condition where it checks if it is in the emulator environment with FUNCTIONS_EMULATOR==true How to detect if environment is development or production