firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

firebase-functions-test cannot connect to an emulator databaseURL #57

Closed filipesilva closed 4 years ago

filipesilva commented 4 years ago

Version info

firebase-functions-test: 0.1.7

firebase-functions: 3.3.0

firebase-admin: 8.9.0

Test case

Make any permissions function that uses Realtime Database, and try to use it in a test script against the emulator. Below the function was called updatePermissionUids.

const test = require('firebase-functions-test')({
  databaseURL: "http://localhost:9000?ns=my-db",
});

// after firebase-functions-test has been initialized
const myFunctions = require('./functions/index.js'); // relative path to functions cod

// Make snapshot for state of database beforehand
const beforeSnap = test.firestore.makeDocumentSnapshot({ foo: 'bar' }, 'document/path');
// Make snapshot for state of database after the change
const afterSnap = test.firestore.makeDocumentSnapshot({ foo: 'faz' }, 'document/path');
const change = test.makeChange(beforeSnap, afterSnap);
// Call wrapped function with the Change object
const wrapped = test.wrap(myFunctions.updatePermissionUids);
wrapped(change);

Steps to reproduce

In one terminal run firebase emulators:start, in another one run node test.js (where test.js is the file containing the test case`.

Expected behavior

No error, and the function runs.

Actual behavior

kamik@RED-X1C6 MINGW64 /d/work/project (security-rules)
$ node fn.js
D:\work\project\node_modules\firebase-functions\lib\providers\database.js:124
            throw new Error('Invalid value for config firebase.databaseURL: ' + databaseURL);
            ^

Error: Invalid value for config firebase.databaseURL: http://localhost:9000?ns=my-db
    at resourceGetter (D:\work\project\node_modules\firebase-functions\lib\providers\database.js:124:19)
    at Function.get (D:\work\project\node_modules\firebase-functions\lib\cloud-functions.js:148:17)
    at hasPath (D:\work\project\node_modules\lodash\lodash.js:6143:24)
    at Function.has (D:\work\project\node_modules\lodash\lodash.js:13150:32)
    at Object.wrap (D:\work\project\node_modules\firebase-functions-test\lib\main.js:28:19)
    at Object.<anonymous> (D:\work\project\fn.js:14:22)
    at Module._compile (internal/modules/cjs/loader.js:774:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    at Module.load (internal/modules/cjs/loader.js:641:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
yuchenshi commented 4 years ago

Quick check: Does it work when you change databaseURL: "http://localhost:9000?ns=my-db", to databaseURL: "http://localhost:9000/?ns=my-db", (with a slash before the question mark)?

laurenzlong commented 4 years ago

Closing due to inactivity.

danipralea commented 4 years ago

I have the same problem. it's driving me crazy. I think I spent the last 4 days on it. do you think we can re-open this @laurenzlong ? @yuchenshi : I personally tried with both approaches. it doesn't work with either.

If I init it without any parameters, I get


    [2020-06-03T10:16:47.159Z]  @firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND\"."} ```
BBX999 commented 2 years ago

I followed the instructions on this page to generate a service key json file to include in the config and I am able to connect to my emulated databases now for testing purposes:

https://firebase.google.com/docs/functions/unit-testing