-
Notifications
You must be signed in to change notification settings - Fork 426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document how to emulate existing functions/triggers. #96
Comments
@larssn thanks for this feedback! I agree with you, it's not obvious how to get started with testing. Just as an overview:
I hope that's helpful for you to get started! cc @markarndt who is in charge of our docs. |
Thank you, it does give a bit more insight into the underlying idea. Assuming my
I wish to test that This is just a small example. Our actual setup is using |
@larssn ah yes I forgot to mention For your situation I would write a test that looks like this. Please excuse any errors I am coding directly in the GitHub box here haha. The key is to run this test while the Firestore and Functions emulators are running using something like: $ firebase emulators:exec "npm test" Mocha test const testing = require("@firebase/testing");
describe("trigger", () => {
it("does what I expect", async (done) => {
// You could use initializeTestApp if you want to simulate a write from a user (which goes
// through security rules). You need to use your real project ID so that the Functions and
// Firestore emulators can communicate.
const fakeApp = test.initializeAdminApp({
projectId: "YOUR_REAL_PROJECT_ID"
});
const db = fakeApp.firestore();
await db.collection('businesses/foo/products').add({ ... some data ... });
// At this point you need to run a loop or a listener to make sure the function
// does what it's supposed to. The function will excute async after the Firestore write completes.
// For instance, check every 100ms
const intervalId = setInterval(() => {
if (conditionIsMet()) {
clearInterval(intervalId);
done();
}
}, 100);
});
}); |
Thanks Sam, I appreciate it! One last question. Does the emulator intercept the init logic in my main edit |
@larssn if you do It's worth noting that using the empty Also when running inside the emulator you will see |
I'll scrap the service-account, and see if it still works. It's pretty old code from a few years back, and we're not live with what I'm working on here. Anyway, I'm jumping for joy with the stellar support, Sam! 👍 👍 👍 😄 |
@larssn any time! Thanks for your patience. I'll leave this open for docs-improving purposes. |
Am I right in assuming that I still need a Without it, my functions can't access some metadata endpoint in GCP. |
@larssn most of the time you don't need that but sometimes you do. The reason is that Firebase locally authorizes as a user (you) whereas when in production you authorize as a service account. In the past those two things were basically interchangeable with the right scopes but now GCP is starting to reject user tokens for some services. What's the actual endpoint you can't access and / or the error you're getting? Also do you know what code is causing it? |
@larssn hmmm I'm not sure but would you mind moving your last two comments to a new issue? I'd like this one to remain about docs confusion. Also when working inside Cloud Functions, initializing without parameters is preferred: |
@samtstern How to write tests for HTTPS callable function in such a case |
So while I appreciate the quickstart examples, they seem to be lacking in how you'd go about testing existing functions/triggers.
I'm not sure if that's where
firebase-functions-test
comes into play.So consider this a small request for a bit more docs to get people started.
The text was updated successfully, but these errors were encountered: