firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

Online mode redirected to emulators #76

Open melchoir55 opened 3 years ago

melchoir55 commented 3 years ago

I am attempting to follow the tutorial provided here: https://firebase.google.com/docs/functions/unit-testing#online-mode

This code block defines where the firebase services are found on the network, as well as what auth creds to use:

// At the top of test/index.test.js
const test = require('firebase-functions-test')({
  databaseURL: 'https://my-project.firebaseio.com',
  storageBucket: 'my-project.appspot.com',
  projectId: 'my-project',
}, 'path/to/serviceAccountKey.json');

We are trying to write our tests to interact with firebase emulator services. For example, we wish for the test suite to interact with our local emulated version of firestore.

We have been banging our heads trying to get this suite to redirect to local services without success. I would sincerely appreciate any guidance.

itsravenous commented 3 years ago

The firestore emulator runs at http://localhost:8080, so try:

const test = require('firebase-functions-test')({
  databaseURL: 'http://localhost:8080',
  storageBucket: 'my-project.appspot.com', // Emulator doesn't support storage yet
  projectId: 'my-project',
}, 'path/to/serviceAccountKey.json');

And then I think you want to prefix your test command with FIRESTORE_EMULATOR_HOST=localhost:8080 so that Firebase itself knows to connect to the emulator.

With this setup I have successfully written to the local emulated Firestore from a cloud function.

I am, however, having trouble with my specific use case, which is a function triggered by a Firestore onWrite, writing back to the same document that triggered the function. I'll open a separate issue for this.

inlined commented 3 years ago

This is a very cool feature request, but is definitely not trivial. We'll leave it on the back burner for now, but I don't know that we'll ever get to it.

Jaimies commented 3 years ago

@inlined I would argue that this feature is pretty vital at least to my project. Because you need the service account key, you can't run your tests in CI without exposing your API key to the CI provider. You would also have to store the key in your repository for the same reason, which may not always be optimal (you may not want to share the key with everyone who has access to the project).

inlined commented 3 years ago

We're working on support for sensitive secrets. That might solve your needs without being this particular feature request. For better or worse, we're busy with some major things coming down the pipeline and they'll all take precedence over this feature request.

Jaimies commented 3 years ago

@inlined No problem, good luck with those!

FanchenBao commented 2 years ago

I concur that if online mode can be connected to the emulator, that will be great. But I can see that this is not trivial. For one thing, in online mode, the cloud function is not attached to the emulator. This allows debugging on the function code and produce code coverage, for example. But if the function modifies database using the change.after.ref syntax, we cannot figure out a way to test it. As of now, we have to ditch firebase-functions-test and go with full emulation.

odbol commented 2 years ago

I'm also trying to write tests of my firebase functions, but it's giving me errors because (I'm assuming) it doesn't have admin credentials in the emulator. Is that what this bug is about? Is there a way to emulate admin credentials in FirebaseFunctionsTest() using the emulator?