38

Is there a way to run firestore locally (e.g. for testing purposes)?

What would the approach to write tests against the DB (except of using mocks)

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
maku_at
  • 1,579
  • 2
  • 13
  • 21

7 Answers7

22

Update 2020:

There's now also a Firebase Emulator Suite.

Update Nov 2018:

Local emulation, at least for the purpose of testing Firestore rules, was demoed at Firebase Summit 2018 using @firestore/testing and documented under Test your Cloud Firestore Security Rules.

It looks like it's along the lines of:

const firebase = require(`@firebase/testing`)
const app = firebase.initializeTestApp({
  projectId: 'my-project',
  auth: { uid: '123', email: 'name@domain.com' }
})

const attempt = app.firestore()
  .collection('colId').doc('docId').get()
firebase.assertFails(attempt)
firebase.assertSucceeds(attempt)

It seems early-on, as it's not been noted in the release-notes, but I'm sure it's coming along.

Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
19

There is not currently, but stay tuned as it's something we want to provide.

In the meantime we suggest uses a separate testing project to cover this. The daily free tier per project helps with this too.

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
  • Found [info](https://firebase.google.com/docs/functions/unit-testing#mocking_database_writes) for unit testing Realtime DB. Looking forward to something similar for Firestore! – hboylan Jan 13 '18 at 20:07
  • 1
    Hi Dan, I am working with at team trying out Firestore. Do you have any ideas of how we could mock data for firestore when doing e2e tests? We are currently using cypress. – DauleDK Feb 19 '18 at 18:36
8

You can run the Firestore emulator by running:

gcloud beta emulators firestore start

and then set the FIRESTORE_EMULATOR_HOST environment variable as per the console output (e.g. run export FIRESTORE_EMULATOR_HOST=::1:8505).

This requires the Google Cloud SDK and a Java 8+ JRE installed and on your system PATH.

Oliver
  • 11,857
  • 2
  • 36
  • 42
  • If you run start without flags it starts on a different port every time. I also couldn't get that to work with the ::1:xxxx. It did work for me when I passed localhost and a fixed port like `gcloud beta emulators firestore start --host-port localhost:8080` – Thijs Koerselman Sep 22 '19 at 21:31
  • Gcloud seems to be deprecated these days. I was unable to find the modern way to do this. Any idea how? – Waltari Apr 13 '20 at 18:41
2

for a firestore testing write a js example test.js you could test write with this format example

var data = {
        value: {createTime: new Date(),
                updateTime: new Date(),
                fields:{

                        name:{stringValue:'new value data'},
                        age:{integerValue:50}
                      }
        },
        oldValue: {createTime: new Date(),  //old create time
                updateTime: new Date(),  //old update time time
                fields:{

                        name:{stringValue:'olvalue data'},
                        age:{integerValue:50}
                      }
        }
      };
testFireStoreEvent(data);

for run execute

firebase experimental:functions:shell < test.js

UPDATE!!!! VALID FOR WRITE AND UPDATE EVENTS

var data = {
    before: {  
          //your before data

    },
    after: {

        //your after data
     }
  };
testFireStoreEvent(data);
Rene Arias
  • 164
  • 1
  • 7
1

There are two libraries which attempt to facilitate mocking of the firebase sdk.

1) https://github.com/soumak77/firebase-mock
2) https://github.com/mikkopaderes/mock-cloud-firestore

I currently use the first one, since it seems to have a bit more of the SDK implemented.

They're not perfect, but they're currently sufficient for my needs, and are preferable to the other approaches since they're entirely in-process.

Note that firebase-mock (#1) does cause a webpack error if used as-is from Webpack/web code. To resolve, you can use option #2 (mock-cloud-firestore), or use the workaround mentioned here (until a fix gets merged): https://github.com/soumak77/firebase-mock/issues/157#issuecomment-545387665

Other options:

3) Firestore emulator: needs the google-cloud-sdk, and relies on a separate process
4) Separate test project: relies on connection to the internet, which also means possible quota limitations/costs
5) firebase-server: Only supports the realtime-database api, not Firestore

Venryx
  • 15,624
  • 10
  • 70
  • 96
1

Firestore can be setup in local using gcloud.

Start the firestore emulator by running gcloud beta emulators firestore start --host-port=localhost:8081 and if it started successfully you will be seeing Dev App Server is now running

In case if you are using @google-cloud/firestore then create the Firestore instance in this way

// Firestore instance only for test env
const { Firestore } = require('@google-cloud/firestore')
const instance = new Firestore({ projectId; 'Your project id', host: 'localhost', 'port': 8081})
Chandra shekar
  • 240
  • 1
  • 12
1

Now you have an option to work with local firestore emulator by setting local host:

var db = firebaseApp.firestore();
if (location.hostname === "localhost") {
  db.settings({
    host: "localhost:8080",
    ssl: false
  });
}

https://firebase.google.com/docs/emulator-suite/connect_and_prototype#instrument_your_app_to_talk_to_the_emulators

Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146