Functions-samples: Cannot mock config.

Created on 13 Mar 2017  路  9Comments  路  Source: firebase/functions-samples

I'm following your example exactly (I think!) and getting the following when trying to run a jasmine-based test:

functions.config() is not available. Please use the latest version of the Firebase CLI to deploy this function.

Most helpful comment

Did you ensure that you mocked functions.config() BEFORE requiring index.js?

Since the sample code calls functions.config() at a global scope, that call will get made as soon as the file is required. So the mocking needs to happen prior to the require statement.

All 9 comments

It appears to me that the init method in firebase-functions/lib/config.js is looking for either runtimeconfig.json or config.json in very particular places. I was able to get mine running by putting a config.json into the functions directory of my project.

See try-catch statements inside init:
https://github.com/firebase/firebase-functions/blob/master/src/config.ts#L44

Which example are you referring to specifically? Have you tried mocking functions.config() like this: https://github.com/firebase/functions-samples/blob/master/quickstarts/uppercase/functions/test/test.js#L47-L55

Yes, that's the one. I found it here tho: https://firebase.google.com/docs/functions/unit-testing

Did you ensure that you mocked functions.config() BEFORE requiring index.js?

Since the sample code calls functions.config() at a global scope, that call will get made as soon as the file is required. So the mocking needs to happen prior to the require statement.

Would you be able to copy and paste your entire test file here? It's hard for me to troubleshoot without seeing the entire thing. Thanks!

I'm getting the same message but I am not running tests. I am just trying to run my function.
Here is the stack trace:

/usr/local/bin/node /Users/.../functions/index.js
/Users/.../functions/node_modules/firebase-functions/lib/config.js:46
throw new Error('functions.config() is not available. ' +
^

Error: functions.config() is not available. Please use the latest version of the Firebase CLI to deploy this function.
at init (/Users/.../functions/node_modules/firebase-functions/lib/config.js:46:19)
at Object.config (/Users/.../functions/node_modules/firebase-functions/lib/config.js:29:9)
at Object. (/Users/.../functions/index.js:3:31)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:425:7)

Process finished with exit code 1

Hi @m-gant, thanks for chiming in. This is expected behavior. You're not supposed to be able to simply "run your functions" locally without mocking the config. functions.config() is only available within the Cloud Functions runtime, not your local machine.

@laurenzlong Oh, ok that actually makes total sense, but is there a way to test the code and make sure it works before actually deploying it. I have never used JavaScript before and wanted to make sure things were correct before I deployed them. Also, I don't know if this issue is really just due to my own novice and I don't know the scope of your role within Firebase, but I think that this is something that would helpful to include in the documentation.

Hey @m-gant this is probably what you're looking for: https://firebase.google.com/docs/functions/unit-testing. And don't worry at all about scopes and responsibilities when you're interacting with any members of the Firebase team, we work closely together and will route bugs/feature requests to the appropriate component owner. I did incidentally write the documentation on unit testing, so I welcome to any feedback you might have. If you have any other questions about using Cloud Functions for Firebase, the best thing to do would be to post on Stack Overflow and tag it with 'firebase' and 'google cloud functions', we monitor it closely, and it's a better forum for asking usage questions than Github issues, which are reserved for bugs and feature requests.

Was this page helpful?
0 / 5 - 0 ratings