title: Cannot stub admin functions with firebase cloud functions
body: <p>When i try to call the wrapped function on my data I get the following error message:</p>
  <blockquote>
  <p>Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.</p>
  </blockquote>
  <p>My code looks like the following:</p>
  <pre class="lang-js prettyprint-override"><code>const {expect} = require(&quot;chai&quot;)
  const sinon = require('sinon');
  const admin = require('firebase-admin');
  const test = require('firebase-functions-test')()
  
  describe('Unit tests', () =&gt; {
      let myFunctions, adminInitStub;
  
      before(() =&gt; {
          adminInitStub = sinon.stub(admin, 'initializeApp');
          myFunctions = require('../index');
      });
      after(() =&gt; {
          adminInitStub.restore();
          test.cleanup()
      })
      describe(&quot;addSchoolVerified&quot;, () =&gt; {
          it('should create school with value-pair &quot;name&quot;:&quot;placeholder&quot;', async () =&gt; {
              const wrapped = test.wrap(myFunctions.addSchoolVerified)
  
              const data = {
                  name: 'placeholder'
              }
  
              // Call the wrapped function with data and context
              const result = await wrapped(data)
  
              // Check that the result looks like we expected.
              expect(result).to.not.be.undefined
          })
      })
  })
  </code></pre>
  <p>so far I tried using different ways of calling the stub like calling it outside of the before() but that too does result in the above error</p>
  <p><strong>index.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>const functions = require('firebase-functions')
  
  const admin = require('firebase-admin')
  admin.initializeApp()
  
  exports.addSchoolVerified = functions.https.onCall(async (data, context) =&gt; {
      /*if (!context.auth) {
          // Throwing an HttpsError so that the client gets the error details.
          throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
              'while authenticated.');
      }*/
      const schoolName = data.name;
      const writeResult = await admin.firestore().collection('Schule').add({name: schoolName})
      return {result: `School with id: ${writeResult.id} successfully added.`}
  });
  </code></pre>