I've begun using the new firebase-functions-test testing SDK to unit test my Cloud Firestore functions. When I run my tests via npm test
I am getting the following error:
The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services
My method is very similar to the makeUppercase
method found within the quickstarts samples, and thus I have mainly copied the test from the test.js file.
describe('updateActiveNotesCount', () => {
// Test Case: Setting environments/{environment}/users/{userId}/people/{personId}/notes/{noteId}/isActive to 'true' should cause
// .../{personId}/activeNotesCount to be incremented
it('Should increment active notes count on the person field', () => {
// [START assertOffline]
const childParam = 'activeNotesCount'; // The field to set
const setParam = '1'; // the new activeNotesCount
const childStub = sinon.stub();
const setStub = sinon.stub();
// A fake snap to trigger the function
const snap = {
// I believe this represents event params, wildcards in my case
params: {
userId: () => '1',
personId: () => '2',
environment: () => 'dev',
},
// Not ENTIRELY sure how to write this one out
ref: {
parent: {
child: childStub
}
}
};
childStub.withArgs(childParam).returns({ set: setStub });
setStub.withArgs(setParam).returns(true);
const wrapped = test.wrap(myFunctions.updateActiveNotesCount);
return assert.equal(wrapped(snap), true);
// [END assertOffline]
})
});
I am unable to move past this error.
Edit:
I see they now updated the docs to include the new SDK, and they mention config values must be mocked. In my index.js
, I use:
const config = functions.config().firebase
admin.initializeApp(config);
I try to mock it like this now:
before(() => {
// [START stubAdminInit]
test.mockConfig({ firebase: 'FakeId' });
adminInitStub = sinon.stub(admin, 'initializeApp');
myFunctions = require('../index');
// [END stubAdminInit]
});
But no luck.
End Edit
Any help with this would be appreciated.
I had the same issue and no matter how I tried I could not stub initializeApp. The workaround I found was to call admin.initializeApp(functions.config().firebase);
in the beginning of the TEST file.
So, instead of stubbing config
and firebase
and initializeApp
, just do this:
const admin = require('firebase-admin');
const functions = require('firebase-functions');
describe('Cloud Functions', () => {
admin.initializeApp(functions.config().firebase);
before(() => {
const index = require('../index');
This is what worked for me:
Key point: you have to explicitly init the app within the test framework and THEN stub it out
const chai = require('chai');
const assert = chai.assert;
const sinon = require('sinon');
const admin = require('firebase-admin');
const functions = require('firebase-functions');
const test = require('firebase-functions-test')();
describe('Functions', () => {
let _fns, adminInitStub;
before(() => {
test.mockConfig({ YOUR_CONFIG });
admin.initializeApp(functions.config().firebase);
adminInitStub = sinon.stub(admin, "initializeApp");
_fns = require('../index');
});
after(() => {
test.cleanup();
});
describe('when test case blah', () => {
// Test Case: setting messages/{pushId}/original to 'input' should cause 'INPUT' to be written to
// messages/{pushId}/uppercase
it('should do xyz', () => {
assert(1 + 1, 2);
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With