why process.env.FIRESTORE_EMULATOR_HOST will be undefined in test environment? even though I have run the emulator

Viewed 1185

I am using

  • Node 14
  • firebase functions-test: 0.2.3
  • firebase-admin: 9.6.0
  • firebase-functions: 3.13.2
  • firebase tools: 9.10.0
  • mocha: 8.3.2,
  • ts-node: 9.1.1

if I run firebase emulators:start

then I will expect process.env will have properties like

"FUNCTIONS_EMULATOR": "true",
"FIRESTORE_EMULATOR_HOST": "localhost:8080"

now I need to create testing for my cloud function using mocha, I will test it using Firestore and functions emulator, I give my file name events_cron_job.test.ts

after I run the simulator, I try to console log those values and I got undefined like this

enter image description here

I expect

console.log(process.env.FUNCTIONS_EMULATOR)  // will be "true"
console.log(process.env.FIRESTORE_EMULATOR_HOST) // will be "localhost:8080"

I think this is only happen in test environment ( I am using mocha ), I have regular http triggers and it works as expected

before running the emulator, I try to execute this on terminal

export FIRESTORE_EMULATOR_HOST="localhost:8080"
export FUNCTIONS_EMULATOR="true"

and then execute firebase emulators:start , but the result will be the same, I still get undefined for those values

because those values are undefined, then if my laptop is disconnected from the internet, then this testing will not run ( i.e it will only access the data in production server, not the emulator! ), I expect the test will still run even if there is no internet connection since I use emulator.

I run the mocha test using

mocha -r ts-node/register src/tests/cloud_function_tests --recursive --extension .test.ts --timeout 60000 --exit
1 Answers

As you can see in this Github issue comment, in order to run tests with the Firebase emulator you should use the following command to trigger it:

firebase emulators:exec "npm test"

As this is how tests where intended to be executed by the Firebase Team using the emulator.

Related