firebase / firebase-functions-test

MIT License
235 stars 52 forks source link

The default Firebase app does not exist. #18

Closed devganiyahitesh1 closed 6 years ago

devganiyahitesh1 commented 6 years ago

"firebase-functions-test": "^0.1.2", "firebase-admin": "~5.12.1", "firebase-functions": "^1.0.4",

I am creating unit test functions for cloud function which has firestore database operations and running functions locally. Normal functions without database operations are working fine but getting error in this case Error sending message: { Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.

Steps to reproduce `import {assert} from "chai" import as sinon from "sinon" import as test from "firebase-functions-test" import rp = require("request-promise-native"); import * as mocha from "mocha"

const fakeCreatingMessage = { id: "TEST-MESSAGE-ID", from: "Test from id", to: "Test to id", message: "my message", type: "text", create_at: "June 29, 2018 at 3:45:44 PM UTC+5:30", };`

const myTest = test()

after(() => {
    myTest.cleanup
})

//{conversationId}/messages/{messageId}
const onCreateFunWrapped = myTest.wrap(require("../../src/functions/message/onCreate.fun"))

const snap = myTest.firestore.makeDocumentSnapshot({
    id: fakeCreatingMessage.id,
    from: fakeCreatingMessage.from,
    to: fakeCreatingMessage.to,
    message: fakeCreatingMessage.message,
    type: fakeCreatingMessage.type,
    create_at: fakeCreatingMessage.create_at,
}, 'conversations/testconversationId/messages/' + fakeCreatingMessage.id);

it('es request options should be correct', () => {
    let optionsActual;
    const stub = sinon.stub(rp, "post")
    stub.callsFake(options => {
        optionsActual = options
        return "success"
    })

    onCreateFunWrapped(snap, {
        params: {
            conversationId: "testconversationId",
            messageId:fakeCreatingMessage.id
        }
    })

    stub.restore()
    console.log(optionsActual)
})`

And running this functions locally using npm test command. My index.ts file already have added firebase-admin. import * as admin from 'firebase-admin'; admin.initializeApp()

Do i need to add below configurations to resolve above error ? // At the top of test/index.test.js const test = require('firebase-functions-test')({ databaseURL: 'https://my-project.firebaseio.com', storageBucket: 'my-project.appspot.com', projectId: 'my-project', }, 'path/to/serviceAccountKey.json');

laurenzlong commented 6 years ago

Since your cloud function seems to be making real database calls, you'll need to initialize the SDK in "online mode" as described in: https://firebase.google.com/docs/functions/unit-testing#initializing

jfelipeara commented 6 years ago

hi @laurenzlong when i initialize in online mode i get the message that The default Firebase app does not exist and i try to call admin.initializeApp() in the test file and it says that The default Firebase app already exists. in my index file when i have all my functions i have the admin.initializeApp() and it works when i use npm run serve

thechenky commented 5 years ago

@jfelipeara can you provide a code snippet of index.ts and the test file?

jfelipeara commented 5 years ago

@thechenky i managed to make it work. The problem was that i was trying to call admin.firestore() before importing my functions (index file) where the initialization was done. Thanks anyway!

thechenky commented 5 years ago

@jfelipeara glad it works, thanks for the update!

alexandermckay commented 3 years ago

firebase emulators:start --only firestore, functions - results in an error firebase emulators:start --only firestore - no error