13

Using firebase-functions with Node and exports.foo = functions.database.ref('/object').onWrite(event => {});.

I can deploy to the cloud and test just fine ... and I can easily test http-type functions locally using firebase serve --only functions.

However, I don't see a way to test triggers locally. Is it possible? How?

Matt H
  • 6,422
  • 2
  • 28
  • 32

4 Answers4

8

This in now possible. I finally find this clear medium post which explain how do it : https://medium.com/@moki298/test-your-firebase-cloud-functions-locally-using-cloud-functions-shell-32c821f8a5ce

To run a trigger locally, use the "functions shell" tool :

1 - My code for firestore (the medium post use RealTime Database) :

exports.testTrigger = functions.firestore
.document('messages/{messageId}')
.onCreate((snapshot, context) => {
    let message = snapshot.data()
    return snapshot.ref.set({
        ...message,
        status : 'sent' 
    })
})

2 - I run the firebase shell functions in the terminal

firebase functions:shell

3 - I call my "testTrigger" cloud function trigger with data in parameter

testTrigger({text:"hello"})

4- My firestore database has a new "message" updated by my trigger

Damien Romito
  • 9,801
  • 13
  • 66
  • 84
4

Very recently, the Firebase team published an update to the Firebase CLI the added the ability to invoke other types of triggers locally. It's documented here. Be sure you've updated your CLI to the latest version to get this new functionality: npm install -g firebase-tools@latest

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 3
    Not the same thing - invoking a trigger locally `!=` getting the same data you do from a deployed function. What I'd like to see is the locally running server receive the event from the cloud database and invoke the function. What I do now is deploy and watch the logs, which is a pain. – Matt H Oct 08 '17 at 00:43
  • 1
    What you're describing isn't available today, unless you want to write your own code to do it. – Doug Stevenson Oct 08 '17 at 01:24
4

Doug answered it in the comment. While invoking a trigger is probably useful in some cases (thanks Doug), it isn't the same as a deployed trigger, which can't be done yet.

So I'll stick with deploying remotely and watching the remote log ... good 'ole Marco Polo debugging.

Matt H
  • 6,422
  • 2
  • 28
  • 32
  • 5
    Is this possible now? I find the documentation to be a bit ambiguous on this matter, and struggle to get updates when emulating the database triggered functions – Snorre May 14 '19 at 06:20
2

Since May 21st 2020 it's now possible to test triggers locally!

See release blog post https://firebase.googleblog.com/2020/05/local-firebase-emulator-ui.html

Always good to also take a look at the release notes https://firebase.google.com/support/releases#may_21_2020

And note that 8.4 requires Node 10, as discussed in question Update of Firebase CLI to 8.4.0 gives errors about "Unsupported engine" saying '{"node":">=10"}'

Simon B.
  • 2,530
  • 24
  • 30