Unit Testing: The default Firebase app does not exist.
See original GitHub issueEnvironment
- Operating System version: macOS High Sierra 10.13
- Firebase SDK version: 4.6.0
- Library version: 5.4.3
- Firebase Product: any
Problem
Steps to reproduce:
- Create a project with Cloud functions enabled.
- Have at least the following dependencies:
- “firebase”: “^4.6.0”,
- “firebase-admin”: “^5.4.3”,
- “firebase-functions”: “^0.7.1”,
- “jasmine”: “^2.8.0”,
- Write a spec file which utilizes mocking data, e.g.
// onUserEmailWrite.spec.ts
// ...
functions = require('firebase-functions');
spyOn(functions, 'config').and.returnValue({
firebase: {
databaseURL: 'https://not-a-project.firebaseio.com',
storageBucket: 'not-a-project.appspot.com',
apiKey: '...',
authDomain: 'not-a-project.firebaseapp.com',
}
});
admin = require('firebase-admin');
spyOn(admin, 'initializeApp');
myFunctions = require('./onUserEmailWrite');
// ...
event = {
params: {
userId: 'AbmlJhRrdwg2kf6OKwKYlu0lLv52'
},
data: new functions.database.DeltaSnapshot(null, admin, null, 'user@example.com')
};
// ...
spyOn(admin, 'auth').and.returnValue({
updateUser: (uid, properties) => {
expect(uid).toBe(event.params.userId);
expect(properties).toEqual({
email: 'user@example.com',
});
return Promise.resolve();
}
});
await myFunctions.onUserEmailWrite(event);
// ...
// onUserEmailWrite.ts
// ...
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
admin.initializeApp(functions.config().firebase);
// ...
functions.database.ref('/users/{userId}/email').onWrite(async event => {
// ...
const userId = event.data.adminRef.parent.key;
const email = event.data.val();
admin.auth().updateUser(userId, {email: email}); // causes issue
// ...
});
// ...
- Run
jasmine
Changing back firebase-admin to 5.3.0 made my unit tests run fine again. Any 5.4.* version does not resolve the issue.
Relevant Code:
Started
(node:2441) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
F
Failures:
1) onUserEmailWrite can skip if user is being deleted.
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5)
1 spec, 1 failure
Issue Analytics
- State:
- Created 6 years ago
- Comments:18 (9 by maintainers)
Top Results From Across the Web
Firebase Unit Testing: The default Firebase app does not exist
The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services. Throwing in :.
Read more >Build unit tests | Firebase Security Rules - Google
Run local unit tests with the version 9 JavaScript SDK ... If the file does not exist and you don't use the loadFirestoreRules...
Read more >The default Firebase app does not exist. Make sure you call ...
[Solved]-The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services. at FirebaseAppError-node.js.
Read more >Firebase: Troubleshooting “The default Firebase app does not ...
Firebase : Troubleshooting “The default Firebase app does not exist…” ... If like me, you've came across one of the following error while...
Read more >firebase app named '[default]' already exists - You.com
If you want to use that script to automatically initialize Firebase, then you don't need to call initializeApp manually. Just make sure your...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Following worked for me in mocha/sinon world:
Man, that took a while. I’m so glad it got resolved today. Thank you again for the guidance @hiranya911! For anyone who wants a reference in Jasmine: