How to Run Integration Tests with the Firestore Emulator in Google Cloud Build

After a pulling my hair out on and off for months, the answer is docker...
October 14th, 2019  by Blaine Garrett

NOTE: THIS ARTICLE IS IN PROGRESS...

Firestore is a next generation NoSQL database part of Google Cloud Platform.  While Firestore left beta in January of this year and is positioned to replace Datastore, many supporting features are still in Beta and documentation on how it integrates with other Google Cloud Platform products is scarce. One such combo is running the Emulator in Google Cloud Build. My case for this is to run integration tests as part of a CI/CD pipeline. In this post, I will document how to do it as it took me and a few coworkers many many hours to figure this out. 

A couple notes before we begin:

  • I made a github project for this post. You can find it here. It includes a sample app, the tests, and the cloud build config. Swap in your firebase credentials and you should be good to go.
  • This post assumes you are using node and jest tests, but the solution should work for other languages that have firestore clients as well
  • As of this writing, the Firestore Emulator is in Beta and might change
  • We're using gcloud command line tools rather than firebase command line tools so we can later leverage the gcloud docker image in later steps. A similar approach could be achieved using the firebase specific commands.

Pre Requisites:

  • You have a Google Platform account and have Firestore and Google Cloud Build Enabled for a project. In all the code below replace "your-project-id" with your actual project id.
  • You have the gcloud command line tools installed and are using version 268.0.0 or higher. 
  •  You have a browser app with some firestore commands in it. I have included a simple app in the github repo that complements this post.

 

Part 1: Run Your Code Against the Emulator Locally

The firestore emulator runs as a separate process. In a separate terminal from your app, start the emulator with the following command: 
 

$ gcloud beta emulators firestore start --host-port=0.0.0.0:9999

If prompted, install the emulator package.  If all goes well, you should see something like:

$ gcloud beta emulators firestore start --host-port=0.0.0.0:9999

Executing: /google-cloud-sdk/platform/cloud-firestore-emulator/cloud_firestore_emulator start --host=0.0.0.0 --port=9999

[firestore] API endpoint: http://0.0.0.0:9999
[firestore] If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:
[firestore]
[firestore]    export FIRESTORE_EMULATOR_HOST=0.0.0.0:9999
[firestore]
[firestore] Dev App Server is now running.
[firestore]
Start your application with the environment variable in place. This tells the firestore client to talk to your emulator rather than the firebase service. Invoke your firebase queries and ensure your application is working. In the emulator terminal, note that requests are indeed being sent to it.

 

Part 2: Writing Tests

Next we'll write some unit tests to actually run our code locally . Follow this post on how to set up Jest testing to independently run unit and integration tests.

Add a simple test in a file called api.test.js with the contents:

 

 

Add a simple test in a file called api.test.integration.js with the contents:

 

 

 

 

Part 3: Running a Cloud build

TODO:

 

Part 3: Setting up the Emulator Docker Image

TODO:

 

Part 4: Running our Integration Tests against the Emulator

TODO:

 

Part 5: Conclusion

TODO:

 

References:

👍