Reputation: 472
I try to mock my firebase app but at a point, I don't find a solution to make it work and there is no example in the firebase documentation to make it that way.
In my code, I have this method call to initialize firebase:
ServiceAccount can be a JSON path or the string coming from ENV settings for test purpose
initialize(): void {
if (!fs.existsSync(this.serviceAccount) && this.serviceAccount) {
this.serviceAccount = JSON.parse(this.serviceAccount);
}
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(this.serviceAccount),
databaseURL: this.url
});
firebaseAdmin.auth();
firebaseAdmin.database.enableLogging(true);
this.database = firebaseAdmin.database();
}
Then in my test, wrote this:
certStub = sinon.stub(admin.credential, 'cert').returns('');
firebaseAdminInitStub = sinon.stub(admin, 'initializeApp');
authStub = sinon.stub(admin, 'auth')
firebaseDatabaseStub = sinon.stub(admin, 'database');
firebaseDatabaseStub.get(() => (() => ({ref: refStub})));
refStub.withArgs(FirebaseContact.ref).returns({push: pushStub});
pushStub.withArgs(Contact).returns(Promise.resolve({ref: 'new_ref'}));
// Initialize it for the coming test
firebase.initialize();
Basically, I mostly use their code for the real-time database usage, but for the authenticating part, it gave me the error:
The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
Throwing in :
firebaseAdmin.auth();
Exception:
at FirebaseAppError.FirebaseError [as constructor] (node_modules/firebase-admin/lib/utils/error.js:39:28)
addressBook.app_test | at FirebaseAppError.PrefixedFirebaseError [as constructor] (node_modules/firebase-admin/lib/utils/error.js:85:28)
addressBook.app_test | at new FirebaseAppError (node_modules/firebase-admin/lib/utils/error.js:119:28)
addressBook.app_test | at FirebaseNamespaceInternals.Object.<anonymous>.FirebaseNamespaceInternals.app (node_modules/firebase-admin/lib/firebase-namespace.js:105:19)
addressBook.app_test | at FirebaseNamespace.Object.<anonymous>.FirebaseNamespace.app (node_modules/firebase-admin/lib/firebase-namespace.js:372:30)
addressBook.app_test | at FirebaseNamespace.Object.<anonymous>.FirebaseNamespace.ensureApp (node_modules/firebase-admin/lib/firebase-namespace.js:388:24)
addressBook.app_test | at FirebaseNamespace.fn (node_modules/firebase-admin/lib/firebase-namespace.js:268:30)
addressBook.app_test | at Firebase.initialize (src/firebase/firebase.ts:29:23)
addressBook.app_test | at Object.done (src/tests/api/contact.test.ts:43:18)
What I know, initializeApp
should be called to create the app, but as it has been stub (according to the example) it can create an app. So Should I try to stub the auth part or use the initializeApp
differently ?
Any idea ? :(
Upvotes: 2
Views: 2563
Reputation: 472
To make it work I have to refactor my initialize method and move out the config to:
initialize(): void {
if (!fs.existsSync(this.serviceAccount) && this.serviceAccount) {
this.serviceAccount = JSON.parse(this.serviceAccount);
}
firebaseAdmin.initializeApp(this.getConfig());
firebaseAdmin.auth();
this.database = firebaseAdmin.database();
}
getConfig(): Object {
return {
credential: firebaseAdmin.credential.cert(this.serviceAccount),
databaseURL: this.url
};
}
Then, I was able to stub the getConfig like they use in the firebase example, but without stubbing the initializeApp
(It wasn't working with), here is my beforeAll
:
getConfigStub = sinon.stub(firebase, 'getConfig');
getConfigStub.callsFake(() => {});
getConfigStub.returns({
firebase: {
databaseURL: 'https://not-a-project.firebaseio.com',
storageBucket: 'not-a-project.appspot.com',
}
});
firebaseDatabaseStub = sinon.stub(admin, 'database');
firebaseDatabaseStub.get(() => (() => ({ref: refStub})));
refStub.withArgs(FirebaseContact.ref).returns({push: pushStub});
My firebase variable could make you think it comes from the import of the package, it's not. It's my Firebase class used to wrap my logic. see:
import * as fs from 'fs';
import * as firebase from 'firebase';
import * as firebaseAdmin from 'firebase-admin';
import { FirebaseInterface } from './firebaseInterface';
export class Firebase implements FirebaseInterface {
private database: firebaseAdmin.database.Database;
/**
* Constructor
* @param {string} serviceAccount
* @param {string} url
*/
constructor(private serviceAccount: string, private url: string) {}
/**
* Initialize connection
* @returns {firebase.database.Reference}
*/
initialize(): void {
if (!fs.existsSync(this.serviceAccount) && this.serviceAccount) {
this.serviceAccount = JSON.parse(this.serviceAccount);
}
firebaseAdmin.initializeApp(this.getConfig());
firebaseAdmin.auth();
this.database = firebaseAdmin.database();
}
getConfig(): Object {
return {
credential: firebaseAdmin.credential.cert(this.serviceAccount),
databaseURL: this.url
};
}
/**
* Get ref
* @returns {firebase.database.Reference}
*/
getDatabase(): firebaseAdmin.database.Database {
return this.database;
}
}
Upvotes: 1