Open Venryx opened 4 years ago
Anyway, in the meantime, this is what I use to fix it:
// By default, mockFirebaseSdk.firestore() will return a new/different instance each time it's called!
// So, we modify it to return the same singleton-value each time.
window.globalMockFirebaseSdk = new MockFirebaseSdk(); // use this globally
const firestoreSingleton = globalMockFirebaseSdk.firestore();
globalMockFirebaseSdk.firestore = ()=>firestoreSingleton; // always return the same instance
Found a better way to resolve the issue, as seen here:
import {MockFirebaseSdk, MockAuthentication, MockFirestore} from "firebase-mock";
const realTimeDBMock = null;
const authMock = new MockAuthentication();
authMock.autoFlush();
const firestoreMock = new MockFirestore();
firestoreMock.autoFlush();
const storageMock = null;
const messagingMock = null;
const firebaseSdkMock = new MockFirebaseSdk(()=>realTimeDBMock, ()=>authMock, ()=>firestoreMock, ()=>storageMock, ()=>messagingMock);
console.log("Set up Firebase app/sdk mock:", firebaseSdkMock);
While this resolves the issue, I think the default behavior should be changed, such that if no values are supplied for the new MockFirebaseSDK(...)
parameters, they default to "singleton" behavior (consistent with the official API), instead of the current "return new instance each time you call .auth(), .firestore(), etc" behavior.
The official
require("firebase/app").firestore()
function returns a singleton -- that is:However, the firebase-mock equivalent is inconsistent with that:
To be consistent with the official firebase API, shouldn't additional calls to
firestore()
return the same singleton-value each time?(This issue caused a lot of lost time in my project, as the configuration I made to one instance was mysteriously not being "seen" by another section of code, and it turned out to be due to the non-singleton behavior of the firebase-mock
firestore()
equivalent.)