-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error: The default Firebase app does not exist #6
Comments
I'm having a similar issue. |
The initializeApp call has changed from |
See: https://firebase.google.com/docs/functions/beta-v1-diff#functionsconfigfirebase_removed |
Thanks @ahaverty I'll check out the migration stuff and hopefully that resolves this issue! |
I'm running into this also. It is happening because https://stackoverflow.com/questions/48139037/cannot-mock-admin-firestore-during-unit-tests Note his extra comment on the answer:
Surely we're not all supposed to be mocking the entire |
I don't understand this either. Especially since there is an 'online' testing mode that should be able to hit real the firestore api. But in my testing when I reference the |
Hitting the same wall >.< |
@alvico I solved my issue. I had my functions in separate files and each file did not include a call to When I served my functions locally tho, it bombed out because an app can only be initialized once. Therefore, I updated the
|
is it the best / correct way to address that or just a fix in the meanwhile ? |
Here's an alternative fix that worked for me:
That way, if any of the files that i'm importing after the app initialization rely on the default firebase app (like if they initialize the firestore for example), the default app should already be set. I'm using Typescript though, so unsure whether this would work the same way in JS |
This might sound like crazy, But I had the same issue. Then I've changed my "admin.initializeApp();" place like below. Then it works. // const db = admin.firestore(); const firebaseConfig = { const firebase = require('firebase'); firebase.initializeApp(firebaseConfig); |
It did work for me, Thank you ❤️ |
I had the same issue as the OP. All the responses so far seem to be referring to completely different issues which have nothing to do with testing.
It is also similar for other parts such as auth, so if you are going to use offline mode you need to make sure you mock everything. |
I am encountering the same problem and have tried everything above with no luck. At first I had issues with sinon, so I assumed it has something to do with having to stub out .initializeApp() in our test file. But even after getting that to work by manually installing npm sinon, I still received this error. |
Thank you @relair ! Using your stub, I was able to get past the error. I was able to write offline unit tests without modifying the source file. Here's my full firestore mock, which allows simple operations like
|
Version info
firebase-functions-test: 0.1.1
firebase-functions: ^1.0.0
firebase-admin: ~5.11.0
Test case
In index.js I have:
Thus in test.js I have:
Steps to reproduce
Attempt to write a Firestore test that uses the above
before()
and usesmakeDocumentSnapshot()
, like:Expected behavior
Test should run (and fail due to bad assertion) without any errors caused by SDK
Actual behavior
Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services. at FirebaseAppError.Error (native) at FirebaseAppError.FirebaseError [as constructor] (node_modules/firebase-admin/lib/utils/error.js:39:28) at FirebaseAppError.PrefixedFirebaseError [as constructor] (node_modules/firebase-admin/lib/utils/error.js:85:28) at new FirebaseAppError (node_modules/firebase-admin/lib/utils/error.js:119:28) at FirebaseNamespaceInternals.app (node_modules/firebase-admin/lib/firebase-namespace.js:105:19) at FirebaseNamespace.app (node_modules/firebase-admin/lib/firebase-namespace.js:372:30) at FirebaseNamespace.ensureApp (node_modules/firebase-admin/lib/firebase-namespace.js:388:24) at FirebaseNamespace.fn (node_modules/firebase-admin/lib/firebase-namespace.js:328:30) at Object.makeDocumentSnapshot (node_modules/firebase-functions-test/lib/providers/firestore.js:44:45) at Context.it (test/test.js:45:41)
The relevant code within test.js at line 45 is:
The relevant code within firestore.js at line 44 is:
Update: I suspected that
firestore()
onadmin
needs to be mocked so I added:but get the error:
The text was updated successfully, but these errors were encountered: