I am reading how to mock google cloud functions for firebase and have issues of properly mocking the following code:
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();
The example in the link uses the following code to mock initializeApp
which does work
admin = require('firebase-admin');
adminInitStub = sinon.stub(admin, 'initializeApp');
Now admin.firestore is defined in firebase-namespace.js as following:
Object.defineProperty(FirebaseNamespace.prototype, "firestore", {
get: function () {
var ns = this;
var fn = function (app) {
return ns.ensureApp(app).firestore();
};
return Object.assign(fn, require('@google-cloud/firestore'));
},
enumerable: true,
configurable: true
});
I've tried various things to stub this but I fail
Results in
firestore is not a function
:Object.defineProperty(admin, "firestore", { get: function () { return 32; } });
Does not mock firestore() at all and calls the original function which fails hard:
sinon.stub(admin, 'firestore').returns({get() { }});
TypeError: Cannot stub non-existent own property get
firestoreStub = sinon.stub(admin.firestore, 'get').callsFake(function () {return {data:"Foo"}});
I lack understanding what admin.firebase()
actually is. It does not look like it is a property because AFAI when I mock a getter of a property, I would call admin.firebase
and not a function admin.firebase()
. But it is also not mockable via a function.