Comments (5)
Thanks for posting. Based off the path /node_modules/@firebase/firestore/src/util/error.ts:217:5
it looks like your test is hitting the real firestore, so it isn't using the mock. We also don't throw any errors like that, so my guess it comes from firebase directly.
How do you set up the mock in your test suite? Sometimes jest can be frustrating with the way it hoists functions calls, so the mock might be getting called after your app spins up.
from firestore-jest-mock.
So this is how my testing file is setup:
At the top of the file I have:
import { updateUser, currentUserListener, returnFirestore, createNewsArticle, getUserDocument } from './firebase';
import "firebase/auth";
const { mockFirebase } = require('firestore-jest-mock');
const { mockInitializeApp } = require('firestore-jest-mock/mocks/firebase');
mockFirebase({
database: {
users: [
{
id: "a1",
displayName: "Homer Simpson",
phoneNumber: "1234815785",
email: "[email protected]",
dateOfBirth: new Date("10 February, 2000"),
photoURL: "http://google.co.th",
},
{
id: "a2",
displayName: "Bart Simpson",
phoneNumber: "3874384783",
email: "[email protected]",
dateOfBirth: new Date("10 February, 2000"),
photoURL: "http://google.co.th",
},
],
news: [
{
id: "1",
authorName: "Homer Simpson",
datePublished: "2000/01/01",
dateUpdated: new Date(),
category: 'Sports',
hits: 2,
content: "jskl;dfja;lksdjfa",
previewContent: "kdf;alsdkjfa;lskdf",
title: "News Article 1",
}
]
}
});
And this is the start of the test:
describe('The getUserDocument() function', () => {
let firebase;
let firestore;
let autho;
beforeEach(() => {
jest.clearAllMocks();
firebase = require("firebase"); // or import firebase from 'firebase';
autho = firebase.auth();
firestore = firebase.firestore();
});
Now I'm starting to think it's all about variable naming, because now inside the files that stores the function (firebase.js) I have the variables initialized like this: ( the firebaseConfig is of course initialized with real app settings, I'm just replacing it with placeholders so as to not expose my app )
const firebaseConfig = {
apiKey: "MY APP'S KEY",
authDomain: "PROJECTID.firebaseapp.com",
projectId: "PROJECTID",
storageBucket: "PROJECTID.appspot.com",
messagingSenderId: "RANDOMNUMBER",
appId: "APPID",
measurementId: "MEASUREMENTID",
};
const fire = firebase.initializeApp(firebaseConfig);
export const auth = firebase.auth();
export const firestore = firebase.firestore();
And so inside the function inside firebase.js when I call the doc I call it like so:
firestore.doc(
users/${uid}).get();
I'm thinking if I change it to:
firebase.firestore().doc(
users/${uid}).get();
It might allow the mockFirebase to override it. However, I would prefer a way where I didn't have to do that for each and every function.
from firestore-jest-mock.
Ok so update, changing firestore.doc(users${uid}).get(); to firebase.firestore().doc(users/${uid}).get(); doesn't work
However, if I copy the whole function from the file with my firebase functions and paste it into my test file below the describe then that successfully tests it:
describe('The getUserDocument() function', () => {
const getUserDocument = async (uid) => {
if (!uid) return null;
try {
const userDocument = await firestore.doc(`users/${uid}`).get();
return {
uid,
...userDocument.data(),
};
} catch (error) {
console.error("Error fetching user", error);
}
};
let firebase;
let firestore;
let autho;
beforeEach(() => {
jest.clearAllMocks();
firebase = require("firebase"); // or import firebase from 'firebase';
autho = firebase.auth();
firestore = firebase.firestore();
});
This works
from firestore-jest-mock.
If you move the mockFirebase
call into the describe
block, does that fix it? Having it live outside of the test might be causing a scoping issue. Take a look at some of our tests as an example:
https://github.com/Upstatement/firestore-jest-mock/blob/master/__tests__/query.test.js#L12
from firestore-jest-mock.
Closing due to inactivity. Feel free to re-open if you still have questions
from firestore-jest-mock.
Related Issues (20)
- Error in @google-cloud/firestore compatibility example HOT 4
- Module firebase-admin not found, mocking skipped HOT 2
- Firestore instance in snapshot.ref go undefined after make a document query request to the database HOT 1
- QueryDocumentSnapshot that passed to trigger functions doesn't contain createTime element HOT 1
- Firebase Cloud Messaging HOT 2
- Is Firestore mock delete() not yet implemented? HOT 6
- How to test a trigger HOT 2
- Where query on nested fields HOT 6
- Firestore mock for DocumentReference.create() HOT 7
- Checking for mockUpdate within a datasnapshot forEach HOT 3
- React Native Support HOT 3
- include firebase/admin with 'import' instead of 'require' HOT 4
- Example of multiple tests with different database data? HOT 3
- Jest, `mockDoc` validate collection type (v0.18.0)
- Requires wrong package for firebase 9 HOT 4
- firestore and auth mocks not complete on firebase-admin HOT 1
- broken links for firebase doc references
- `set` without merge overrides _collections HOT 1
- Transfer Repo HOT 9
- Is there a way to test multiple firestore states? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from firestore-jest-mock.