functions-samples icon indicating copy to clipboard operation
functions-samples copied to clipboard

Cannot mock config

Open jliukai opened this issue 6 years ago • 3 comments

following your "online mode" example.

given the following files .runtimeconfig.json //my env keys stored here foo.js test/test.js test/myProjServiceKey.json //from gcp > Iam > service account, app engine

in my code:

//foo.js
const functions = require('firebase-functions');
const config = functions.config(); //ERRORS HERE my line 23.
console.log(config.somefoo.key); //prints out a key so i know its setup
//test/test.js
const config = require('../.runtimeconfig.json');
const projectConfig = { projectId: '<my project>' };  //inserted my project id
const test = require('firebase-functions-test')(projectConfig, './myProjServiceKey.json');   test.mockConfig({ config }); //also tried manually entering in values.
console.log(config.somefoo.key); //prints out a key so i know its setup

//...later in the code
describe('not working Test3  ', () => {
    before(() => {
        const myFoo = require('../foo'); //ERRORS HERE my line 106
    });
    after(() => { 
        test.cleanup();
    });
    describe('...', function () {
        it('...', () => {
            //const wrap = test.wrap(myFoo.run); //run() inside class Foo.
        });
    });

});

running npm test, I get an error

> Error: Firebase config variables are not available. Please use the latest version of the Firebase CLI to deploy this function.
>  at init (node_modules/firebase-functions/lib/config.js:55:15)
>       at Object.config (node_modules/firebase-functions/lib/config.js:29:9)
>       at Object.<anonymous> (foo.js:23:26)
>       at require (internal/module.js:11:18)
>       at Context.before (test/test.js:106:27)

I made sure, I mocked functions.config() BEFORE requiring foo.js

anything else im missing?

jliukai avatar Apr 18 '18 23:04 jliukai

@jliukai have you found a solution?

atereshkov avatar Apr 02 '20 17:04 atereshkov

I just ran into this issue as well. Running firebase-functions-test@0.2.1. My config is always {}; mockConfig is called before any other requires. Any ideas how to fix this?

mariotacke avatar May 07 '20 04:05 mariotacke

I got mine to work by trying to set the config values manually and seeing this error: Error: Cannot set to reserved namespace firebase. I tried setting the api key to firebase.apikey variable in my mock config which apparently caused this issue. When I instead mock project.apikey it works just fine. I wish the error message was more verbose; maybe I just missed it 🤷

mariotacke avatar May 07 '20 04:05 mariotacke