-
Notifications
You must be signed in to change notification settings - Fork 2
Firebase Emulator Tutorial
Here is a simple and straight forward tutorial on how to use the Firebase emulator suite. To see the full documentation, click here.
The Firebase emulator suite is a Firebase "server" that is locally emulated on your device. This serves multiple purposes, mainly : end to end testing. Testing on the production database is a bad idea for a multitude of reasons: networking issues, incorrect tests corrupting our database, security reasons, etc.... This is why we opted for the emulated one. Thanks to it we can run tests without mocking any Firebase dependencies, all while being able to test how the behavior of our app will function on a real database.
First of all make sure that you have the correct prerequisites:
-
Node
andnpm
.npm
is a package manager forJavascript
(just like pip for python) and it's necessary to download the emulators from the Firebase CLI (command line interface).Node
is the interpreter forJavascript
. To check if they are already installed you can run :node -v
npm -v
to see the version. If it doesn't work look at this tutorial. - Java (you should all have it).
- The Firebase CLI (command line interface). They can be downloaded here (Note: Every time i've done this, the version was much more stable using
npm
to download the CLI, so I recommend it). To check that it is working, you can run this command :firebase --version
in a terminal. If it prints the version, everything should be set. To be extra sure you can runnpm install -g firebase-tools
to get the latest version of the CLI (necessary for the new UI).
Now that you have everything set up, we can start the installation process of the emulator. Just make sure you are in a different branch and have committed your work, so you can always revert if anything goes wrong :).
- Firstly, open your terminal and go to the working directory of the project. This should be
/Unio
. - Then you can write
firebase init
. The following screen should appear. (Note if you have not signed in yet it will ask you to do so, do it, and with the same credentials you use with firebase) :
- Select
yes
. - Now it should ask what features you want to download. Choose the following (Firestore, Functions, Emulators, Storage) :
Note: Functions serves no purpose, but that's how we've done it and it works 👍
- Then it will ask you if you want to link it to an existing project, if you've logged in with your Firebase credentials, you should have the option to select the
unio-1b8ee
project. Do so.
- Continue setting up everything by default.
- When it asks what language to use for cloud functions, select
JavaScript
, and ESLint is optional. -
Install depencies with
npm
when it asks you to do so. - When that is done, the prompt should ask you to select what emulators you want to use. Select : Firestore, Auth, Functions, Storage.
- It will now prompt you to select the ports used for your emulators, select all your default ones. This is important because the tests are linked to them so if you choose other ones, the tests will not pass.
- For the emulator UI select yes, this is the whole beauty of the new Firebase emulator suite ;). And again, for the port, leave it as default.
After all this, your emulator should be set up.
Now that your emulator is installed, you can start it up with the following command : firebase emulators:start
, from the directory of your project. You should get the following screen in your terminal:
You can make sure everything is running correctly by opening this URL in your web browser : http://127.0.0.1:4000/
. This should display the following screen :
This is your local emulator running on your computer ⭐.
With the additions of new end-to-end tests that require the emulators to be initialized with mocked data (typically the Search end to end test), you now need to start the emulators with this command:
firebase emulators:start --import=firebase/emulator-data
in the terminal.
This will launch the emulators with the data found in the firebase/emulator-data
directory.
When looking at your local emulator UI you should see some mocked associations, events and users already initialized in the Firestore database.
Note: please do restart your emulator every time you run your tests, this is to ensure that the DB is initialized with the correct settings, this will be fixed in the future but for now it simulated the behavior of the exec
function just like the CI does.
If you reached this step, this means that everything is working fine and the test should pass 🤞. If you have any problems during this process, you can contact @AlouchLaBouche or @oskar-codes or check out the official documentation.
If you're wondering, how the tests are linked to the mocked Firebase database. It is done in the setup of EndToEndTest.kt
found in the authentication package in the UI tests.
The line Firebase.firestore.useEmulator("10.0.2.2", 8080)
links the Firebase.firestore
object to the locally run emulator instead of the production one. All subsequent requests go through the emulated one. The IP address "10.0.2.2"
is a special IP for the emulated Android device that redirects to the correct process on your computer (the emulated Firebase). This is why these tests do not pass if ran on a external phone, as the IP address means nothing to it.