Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase-mock's firestore() function does not return a singleton #156

Open
Venryx opened this issue Oct 23, 2019 · 2 comments
Open

Firebase-mock's firestore() function does not return a singleton #156

Venryx opened this issue Oct 23, 2019 · 2 comments

Comments

@Venryx
Copy link

Venryx commented Oct 23, 2019

The official require("firebase/app").firestore() function returns a singleton -- that is:

import firebase from "firebase/app";
firebase.initializeApp(/* ... */);
assert(firebase.firestore() === firebase.firestore()`); // always succeeds

However, the firebase-mock equivalent is inconsistent with that:

import {MockFirebaseSdk} from "firebase-mock";
const firebase = new MockFirebaseSdk();
assert(firebase.firestore() === firebase.firestore()); // always fails

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.)

@Venryx Venryx changed the title new MockFirebaseSdk().firestore() does not return a singleton Firebase-mock's firestore() function does not return a singleton Oct 23, 2019
@Venryx
Copy link
Author

Venryx commented Oct 23, 2019

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

@Venryx
Copy link
Author

Venryx commented Oct 23, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant