firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

Cannot connect to database emulator #64

Open danipralea opened 4 years ago

danipralea commented 4 years ago

Version info

firebase-functions-test: 0.2.1**

firebase-functions: 3.6.2**

firebase-admin: 8.12.1**

Hello and thank you for reading this. I am struggling for the past few days around this, but I just can't make it work. It all boils down to not being able to connect firebase-functions-test to my realtime database emulator.

Prior to everything, I do:

const projectId = "not-a-project";
const databaseURL = `http://localhost:9000/?ns=${projectId}`;
process.env.GCLOUD_PROJECT = projectId;
process.env.FIREBASE_DATABASE_EMULATOR_HOST = 'localhost:9000';
process.env.FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'localhost:9000';

I have my admin app successfully updating and retrieving data, so that's fine:

const adminApp = () => {
  return firebase.initializeAdminApp({ projectId: projectId, databaseName: projectId }).database();
};

but the issue is with firebase-functions-test package:

const projectId = "not-a-project";
const databaseURL = `http://localhost:9000/?ns=${projectId}`;
const testEnv = functions({
  projectId: projectId,
  databaseURL: databaseURL
});

If I do this, I get Invalid value for config firebase.databaseURL: http://localhost:9000/?ns=not-a-project

If I do this:

const testEnv = functions();

I get [2020-06-03T10:36:01.235Z] @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\"."}

If I do this:

const testEnv = functions({
  projectId: projectId
});

I get Missing expected firebase config value databaseURL, config is actually{"projectId":"not-a-project"} If you are unit testing, please set process.env.FIREBASE_CONFIG

tettoffensive commented 4 years ago

@danipralea Did you figure out any workaround?

danipralea commented 4 years ago

@danipralea Did you figure out any workaround?

nope, didn't knew how to get around it. I talked with someone at the firebase support team and he said it's working like a charm for him with this version:

const projectId = "project-Id";
const databaseURL = "http://localhost:4000/database";

const test = require('firebase-functions-test')({
  databaseURL: databaseURL,
  projectId: projectId,
});

but for me it still doesn't work. maybe it helps you or anyone else

Ciberusps commented 4 years ago

Are u sure that in your functions databaseURL same as in your tests? I fixed this one and now all works fine image