question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit Testing: The default Firebase app does not exist.

See original GitHub issue

Environment

  • 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:

  1. Create a project with Cloud functions enabled.
  2. Have at least the following dependencies:
  • “firebase”: “^4.6.0”,
  • “firebase-admin”: “^5.4.3”,
  • “firebase-functions”: “^0.7.1”,
  • “jasmine”: “^2.8.0”,
  1. 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
  // ...
});
// ...
  1. 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:closed
  • Created 6 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

9reactions
hiranya911commented, Oct 26, 2017

Following worked for me in mocha/sinon world:

    const updateUserStub = sinon.stub().returns('done');
    // admin.auth must return a callable object, so that admin.auth() would work.
    sinon.stub(admin, 'auth').get(function getterFn(){
        return () => {
            return {updateUser: updateUserStub};
        };
    })
2reactions
zgosalvezcommented, Oct 26, 2017

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:

spyOnProperty(admin, 'auth', 'get').and.returnValue(() => {
  return {
    updateUser: (uid, properties) => {
      expect(uid).toBe(event.params.userId);
      expect(properties).toEqual({
        email: 'user@example.com',
      });

      return Promise.resolve();
    }
  };
});
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found