"The default Firebase app does not exist." when test Cloud Function with Admin SDK in firebase-functions-test

Viewed 58

First I trying to get default firebase app but it throw: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.

import { getDatabase } from 'firebase-admin/database';
import { getFirestore, Timestamp } from 'firebase-admin/firestore';

let defaultDatabase = getDatabase();
let defaultFirestore = getFirestore();

Then I try:

import { getDatabase } from 'firebase-admin/database';
import { getFirestore, Timestamp } from 'firebase-admin/firestore';
import { initializeApp } from 'firebase-admin/app';

const app = initializeApp();

let defaultDatabase = getDatabase(app);
let defaultFirestore = getFirestore(app);

But above code throw "Can't determine Firebase Database URL.".

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',
    databaseURL: "https://project-id.asia-southeast1.firebasedatabase.app",
}, 'serviceAccount.json');


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

I already struggle it for a day, thanks for any help.

0 Answers
Related