-3

I'd like to run tests that call multiple Cloud Functions in the emulator that make use of a mocked out external service (getstream.io). That means the mock would have to stay around across function invocations. Is something like this possible?

let mock = new SomethingMock();

exports.resetMock = functions.https.onCall((data, context) => {
  mock = new SomethingMock();
});

exports.addActivity = functions.https.onCall(async (data, context) => {
  await mock.addActivity(something);
});

exports.getActivities = functions.https.onCall((data, context) => {
  // assumes addActivity has been called a few times
  return mock.getActivities();
});

This page says gives no guarantees about preservation of global state in production, but says nothing about the emulator: https://firebase.google.com/docs/functions/tips#use_global_variables_to_reuse_objects_in_future_invocations

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Seems this question is getting downvoted. Is it not clear enough? Or is it because the docs hint at something, but I was searching for a more explicit answer? Any suggestions on how I can improve my contributions here are more than welcome. – Vincent den Boer Apr 27 '21 at 09:33
  • Also, this is something also I didn't solve yet. I'm thinking about spinning up an external Node.js server to mock out the dependencies to work around it. So I also can't write an answer that I can accept yet. – Vincent den Boer Apr 27 '21 at 09:34

1 Answers1

0

The firebase emulator will neither guarantee the preservation of global state.
The answer is in the definition itself:

The Firebase Local Emulator Suite consists of individual service emulators built to accurately mimic the behavior of Firebase services. This means you can connect your app directly to these emulators to perform integration testing or QA without touching production data. They(emulatored firebase services) are built for accuracy, not performance or security, and are not appropriate to use in production.

Having said that, there is not much added value in having an emulator that does not return results as if the service was in production.

Antonio Ramirez
  • 943
  • 6
  • 15
  • Although that text hints toward this being the case, it does not directly confirm this particular behavior is mimicked exactly. Are you one of the developers of the emulator? – Vincent den Boer Mar 10 '21 at 15:41
  • If there is indeed no way to preserve global state, I can hack around it starting up an external Node.js service, but it would be great if there was some way to prevent this, even by having some special object I can assign properties to or something. – Vincent den Boer Mar 10 '21 at 15:44
  • No, I am not a developer. If you want a direct answer from the firebase team, then you should open a github issue. https://github.com/firebase/firebase-tools-ui/issues – Antonio Ramirez Mar 10 '21 at 20:17