Can I run Firebase Storage cloud functions locally? - firebase

From Firebase DOCS:
Run functions locally
The Firebase CLI includes a Cloud Functions emulator which can emulate the following function types:
HTTPS functions
Callable functions
Cloud Firestore functions
You can run functions locally to test them before deploying to production.
QUESTION
Is there a way to run cloud functions for Firebase Storage (ex: triggered by file upload) locally or do I need to actually deploy them for testing?

firebaser here
Update: the Firebase Emulator Suite now also supports Cloud Storage. This includes uploads to emulated Cloud Storage triggering emulated Cloud Functions.
See the documentation for full details.

Storage has been implemented in firebase tools 9.11.0. I just took it for a test run, and I found that functions running in the emulator are, indeed, triggered by uploads to the emulated storage.
https://github.com/firebase/firebase-tools/issues/1738#issuecomment-843256335

Related

I cannot run my Firebase storage functions on the Firebase emulator

Today i ran into a problem with my Firebase Cloud Function that generates images sizes when new images are being uploaded to the Firebase Storage. This problem causes my quota to exceed after a couple uploads and when the quota exceeds the limit i am not able to use firebase anymore. Now i am trying to run this Cloud Function locally so that my quota will not exceed.
I tried running the emulator in my terminal with this command: firebase emulators:start
This initializes all my Cloud Functions except my Firebase Auth and Firebase Storage functions. The firebase docs say the emulator only supports:
HTTPS functions
Callable functions
Cloud Firestore functions
Is there a way/work around so i can run my Firebase Storage and Firebase Auth functions locally?
From the emulator, no. As you've seen, they're just not supported.
Look into invoking functions using the functions shell, or unit testing your functions with the functions-test module.

Firebase trigger function ignored due to missing "firebaseauth.googleapis.com emulator"

I have a Firebase function createUser that is triggered by auth.user().onCreate.
Whenever I serve functions locally, it is ignored.
I get the same output when I run firebase serve, firebase serve -only functions,firestore, firebase emulators:start, or firebase emulators:start --only functions,firestore:
⚠ functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to /home/david/Sync/alikely/key.json. Non-emulated services will access production using these credentials. Be careful!
✔ functions[createItem]: http function initialized (http://localhost:5000/alikely-ce1d5/us-central1/createItem).
i functions[createUser]: function ignored because the firebaseauth.googleapis.com emulator does not exist or is not running.
As shown above, I have attempted to follow the instructions to setup admin credentials here: https://firebase.google.com/docs/functions/local-emulator.
I have initialized functions and firestore with firebase init, and my project directory contains a firestore.rules, firestore.indexes.json, and a firebase.json. Are the contents of one of these files to blame?
How can I locally emulate all of my functions for full local development? How can I enable the "firebaseauth.googleapis.com emulator"?
The documentation you linked says:
The Firebase CLI includes a Cloud Functions emulator which can emulate
the following function types:
HTTPS functions
Callable functions
Cloud Firestore functions
Auth functions are currently not supported, just the three types mentioned above.

How can I trigger local Firestore Cloud Functions using the emulator?

I've just set up the Emulator per the instructions here:
Dan$ firebase emulators:start --only functions
Starting emulators: ["functions"]
✔ functions: Emulator started at http://localhost:5001
i functions: Watching "/Users/danfein/Desktop/reviews/backend/functions" for Cloud Functions...
I understand there's something running at :5001. My local DB environment is not running locally (as I understand that's not something supported by Firebase) so because my connections are going out to the web, I don't understand how to trigger something to :5001.
How can I, with my running React instance (running on :3000 if relevant), trigger one of the cloud functions currently living in my functions folder.
The functions do currently run and are appropriately triggered in the cloud.
You will want to set up your development environment as described in the documentation. There is a new emulator suite that ties together Cloud Firestore (which actually can be emulated) and Cloud Functions.

How to do integration tests for Firebase HTTP functions with Firestore

Trying to write integrations tests for HTTPS function (implemented as an express app) that use Firestore as DB
Since its an integration test, I don't want to mock the Firestore DB in this case, however, since they perform network calls, they take time to execute
Is there a Firestore local emulator to use in this scenario? There is an option to config Firestore in offline mode, maybe that's the way? Didn't find any documentation in Firebase on this use case
AskFirebase
You have to setup a Firestore emulator locally (assuming you already have firebase-cli installed):
$ firebase setup:emulators:firestore
Then run the emulator:
$ firebase serve --only firestore
With the emulator running, you can run your test suites.
In order to write tests you can use the #firebase/testing package. For more information, check the official documentation here.
There is also an official quickstart repository on GitHub, which shows how to test Firestore locally, both using JavaScript and Typescript. You can find it here.

update Firebase emulator function timeout duration

When emulating firebase functions locally is there a way to change the function timeout from the default 60s?
--idlePruneInterval has been added as an option to the google cloud emulator CLI: https://github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/66
This option doesn't appear to work is not officially supported in either the firebase serve mode:
firebase serve --only functions --idlePruneInterval=5000
or the experimental shell emulation:
firebase experimental:functions:shell --idlePruneInterval=5000
Is there another way around this problem? Perhaps editing a config file?
The Cloud Functions emulator from Firebase is separate from the one provided by the Google Cloud Platform org. The command line parameters from one won't necessarily work on the other.

Resources