When i try to call the wrapped function on my data I get the following error message:
Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
My code looks like the following:
const {expect} = require("chai")
const sinon = require('sinon');
const admin = require('firebase-admin');
const test = require('firebase-functions-test')()
describe('Unit tests', () => {
let myFunctions, adminInitStub;
before(() => {
adminInitStub = sinon.stub(admin, 'initializeApp');
myFunctions = require('../index');
});
after(() => {
adminInitStub.restore();
test.cleanup()
})
describe("addSchoolVerified", () => {
it('should create school with value-pair "name":"placeholder"', async () => {
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
})
})
})
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
index.js
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()
exports.addSchoolVerified = functions.https.onCall(async (data, context) => {
/*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.`}
});