You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using React Native with the react-native-firebase package and tried using firestore-jest-mock to mock my database. Currently, my tests fail when using the mock as the documentation suggests. The documentation for these two packages do not align.
Whereas firestore-jest-mock documentation suggests importing an injected Firestore stub that does not exist in @react-native-firebase/firestore then calling its constructor.
// mockReactNativeFirestore called abovetest('testing stuff',()=>{const{ Firestore }=require('@react-native-firebase/firestore');// Doesn't normally exist in this packageconstfirestore=newFirestore();// Notice we're not calling firestore as a functionreturnfirestore.collection('users').get().then(userDocs=>{expect(mockCollection).toHaveBeenCalledWith('users');expect(userDocs.docs[0].data().name).toEqual('Homer Simpson');});});
I am not familiar with the @google-cloud/firestore package and how the documentation suggests we use it, so I'm not sure how different it is from react-native-firebase. I utilize other parts of the API like FieldValue and FieldPath in my production code. When using mockReactNativeFirebase the code I wanted to test would fail to load, saying these were all undefined or that firestore is not a function.
Steps to reproduce
Create new React Native project
Install @react-native-firebase/firestore and firestore-jest-mock
Set up Jest and Firestore mock using firestore-jest-mock documentation
Create tests for firestore using @react-native-firebase/firestore documentation
firestore.test.ts
import{mockCollection}from'firestore-jest-mock/mocks/firestore';importfirestore,{firebase}from'@react-native-firebase/firestore';describe('firestore-jest-mock tests',()=>{it('gets a user',async()=>{awaitfirestore().collection('users').doc('test').get();expect(mockCollection).toHaveBeenCalledWith('users');});it('creates and uses an increment function',async()=>{constincrementFunction=firebase.firestore.FieldValue.increment(1);awaitfirestore().collection('users').doc('test').set({followerCount: incrementFunction},{merge: true});expect(mockCollection).toHaveBeenCalledWith('users');});});
firestoreMock.setup.ts
import{mockReactNativeFirestore}from'firestore-jest-mock';mockReactNativeFirestore({database: {users: [{id: 'test',followerCount: 0,},],},});// Needed to get Jest working in a React Native projectjest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');
Expected result
Firestore tests pass.
Actual result
Tests fail after using mockReactNativeFirestore({ database: { ... }}).
When running Jest tests:
...
TypeError: (0 , _firestore2.default) is not a function
4 | describe('firestore-jest-mock tests', () => {
5 | it('gets a user', async () => {
> 6 | await firestore().collection('users').doc('test').get();
...
TypeError: Cannot read properties of undefined (reading 'firestore')
8 | });
9 | it('creates and calls an increment function', async () => {
> 10 | const incrementFunction = firebase.firestore.FieldValue.increment(1);
...
The same errors appear when trying to render components that reference firestore() during tests as well.
Environment
Node version: 20.11.1
React Native version 0.74.3
React version 18.2.0
@react-native-firebase/app: "^20.1.0",
@react-native-firebase/firestore: "^20.1.0",
firestore-jest-mock: "^0.25.0"
Potential Fix
I have already created a patch using patch-package to fix these errors in my project:
Notice that instead of returning the firestoreStub directly, it now returns as an esModule with a default method so that we can use firestore() how @react-native-firebase/firestore suggests. I also added the firebase object so that it matches the exports of @react-native-firebase/firestore. For example, the suggested way to use FieldValue and FieldPath are like so:
With my patch all of these tests pass now with no changes to my production code. I'd like to make this change a PR, as well as updating documentation and test cases. I looked at the tests and this would break every mockReactNativeFirestore test in full-setup-library-firestore.test.js but only require minor, repetitive changes. Forgive me if this is the wrong way to suggest this change.
The text was updated successfully, but these errors were encountered:
Description
I am using React Native with the react-native-firebase package and tried using firestore-jest-mock to mock my database. Currently, my tests fail when using the mock as the documentation suggests. The documentation for these two packages do not align.
The react-native-firestore package documentation suggests importing
firestore
as the default export like so and callingfirestore
as the default function:Whereas firestore-jest-mock documentation suggests importing an injected Firestore stub that does not exist in
@react-native-firebase/firestore
then calling its constructor.I am not familiar with the
@google-cloud/firestore
package and how the documentation suggests we use it, so I'm not sure how different it is from react-native-firebase. I utilize other parts of the API likeFieldValue
andFieldPath
in my production code. When usingmockReactNativeFirebase
the code I wanted to test would fail to load, saying these were all undefined or thatfirestore
is not a function.Steps to reproduce
@react-native-firebase/firestore
andfirestore-jest-mock
firestore-jest-mock
documentation@react-native-firebase/firestore
documentationfirestore.test.ts
firestoreMock.setup.ts
Expected result
Firestore tests pass.
Actual result
Tests fail after using
mockReactNativeFirestore({ database: { ... }})
.When running Jest tests:
The same errors appear when trying to render components that reference
firestore()
during tests as well.Environment
Potential Fix
I have already created a patch using
patch-package
to fix these errors in my project:patches/firestore-jest-mock+0.25.0.patch
Notice that instead of returning the
firestoreStub
directly, it now returns as an esModule with a default method so that we can usefirestore()
how@react-native-firebase/firestore
suggests. I also added thefirebase
object so that it matches the exports of@react-native-firebase/firestore
. For example, the suggested way to useFieldValue
andFieldPath
are like so:With my patch all of these tests pass now with no changes to my production code. I'd like to make this change a PR, as well as updating documentation and test cases. I looked at the tests and this would break every mockReactNativeFirestore test in
full-setup-library-firestore.test.js
but only require minor, repetitive changes. Forgive me if this is the wrong way to suggest this change.The text was updated successfully, but these errors were encountered: