Mock library for Cloud Firestore
Install the library by running this command:
npm install --save-dev mock-cloud-firestore
Import the mock-cloud-firestore.js
file from the dist
folder in your app
Import the library by using require('mock-cloud-firestore')
MockFirebase
class is provided to replace your app's Firebase instance
const firebase = new MockFirebase(fixtureData);
Replacing the Firebase instance depends on how your app consumes it. As an example, you could do
window.firebase = new MockFirebase(fixtureData)
on a browser environment.
Here's a sample fixture data
const fixtureData = {
__collection__: {
users: {
__doc__: {
user_a: {
age: 15,
username: 'user_a',
__collection__: {
friends: {
__doc__: {
user_b: {
reference: '__ref__:users/user_b'
}
}
}
}
},
user_b: {
age: 10,
username: 'user_b',
__collection__: {
friends: {
__doc__: {
user_a: {
reference: '__ref__:users/user_a'
}
}
}
}
},
user_c: {
age: 20,
username: 'user_c'
}
}
}
}
}
Here's whats going on with the example above
- There will be a
users
collection - There will be
user_a
,user_b
, anduser_c
documents underusers
collection - There will be a
friends
subcollection underuser_a
anduser_b
documents __ref__:
indicates that this is aReference
data type to a document__ref__:users/user_a
will be equivalent tofirestore.collection('users').doc('user_a')
Not all APIs are supported. Here are some unsupported major ones
- Transaction
onSnapshot()
is supported but doesn't get realtime updates
You can look into the source code to see if an API you're using is supported. I've written it in a way that you could quickly scan the APIs.
git clone <repository-url>
this repositorycd mock-cloud-firestore
npm install
npm test