I am running my firebase tests using the following command
firebase emulators:exec --ui 'mocha --reporter spec --timeout 10000'
but the ui closes and emulators shut down as soon as the test finishes. I have looked at the params in the help but cant seem to find a way to keep them running so I can check values in the db.
I have tried starting the emulators first and then running with exec but it always complains that emulator instances are already running.
Is there a method of doing this?
You can try starting the firebase emulators:start and then running your test script after it has started
mocha --reporter spec --timeout 10000
It fails when you run exec after starting because the ports assigned to the emulators are busy.. Instead of running exec after the emulators have started, run the test script itself
Related
I got a Discord JS project running which works fine, now I wanted to also run a simple express script to receive a post request. When I do "node ." the code works and successfully logs the requests, if I do "pm2 start index.js --name xyz" the code also starts, logs its boot up message, doesn't shut down, but also doesn't react to any requests. There is simply no response at the set port by express.
Running on an Ubuntu VPS.
Works if I do "pm2 start index.js --name xyz -- --port XXXX" I don't know why and would like to know how these extra arguments work if anyone could explain.
You can read about arguments on the official docs
It's likely you have a runtime error. Please read your own logger files and ~/.pm2/logs/*.log and see if you can find anything. Had it been a syntax error, it would have shut down immediately. Yet, always pm2 statusafter start to double check it.
We have a NodeJS application that depends on an external database. Currently I run the mocha tests in one of two ways:
manually start a docker container, run the tests, shutdown the container
on merge using a github action (service works like a charm)
What I would like to achieve:
Being able to run my tests using npm run testsuite that starts my database container, run the mocha test and shut down the container
On investigation I found plenty of tutorials how to run tests inside a container using docker or docker-compose, but nothing how to "just" launch a temp container for the database only.
Help is very much appreciated
Encapsulate your test like so in your package.json:
{
"scripts": {
"test": "docker-compose up -d && mocha && docker-compose down"
}
}
The -d flag will allow the shell to keep processing commands after the container starts. Otherwise the docker container will indefinitely block your shell (and your tests). In case you're not using docker compose, you can always run:
docker run -d
Hopefully that helps!
I'm assuming you may be starting the test using mocha runtime with npm test -- if that's the case you may want to try starting the mocha tests by calling the API directly from a node file.
In your wrapper Node file you can try and start up the container, then start Mocha.
//start container
const mocha = new Mocha();
//more config
mocha.run( ...etc )
//shut down container
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
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
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.
I was trying out the new Firebase Emulators UI announced on May 21, 2020. In the Firebase Docs, I noticed that there is a CLI command that allows us to run a script file:
firebase emulators:exec scriptpath
Run the script at scriptpath after starting emulators for the
Firebase products configured in firebase.json. Emulator processes will
automatically stop when the script has finished running.
Although the docs does not mention what kind of script file it should be, I presume it could be either a test script or a workable JavaScript file that be executed standalone with node filename.js without any errors. This makes sense because I can actually run an initial JS file that pre-populate some test data to the Firestore Emulator for further testing.
But since I've previously started the emulators, I have to open a new instance of command window to run the firebase emulators:exec command. Unfortunately, I hit the following errors:
D:\Firebase\my-project>firebase emulators:exec setup-db.js
i emulators: Starting emulators: functions, firestore, database, hosting, pubsub
! hub: emulator hub unable to start on port 4400, starting on 4401 instead.
! emulators: It seems that you are running multiple instances of the emulator suite for project my-project-id. This may result in unexpected behavior.
i emulators: Shutting down emulators.
i hub: Stopping emulator hub
! functions: Port 5001 is not open on localhost, could not start Functions Emulator.
! functions: To select a different host/port, specify that host/port in a firebase.json config file:
{
// ...
"emulators": {
"functions": {
"host": "HOST",
"port": "PORT"
}
}
}
i emulators: Shutting down emulators.
Error: Could not start Functions Emulator, port taken.
Take note of this line:
! emulators: It seems that you are running multiple instances of the emulator suite for project my-project-id. This may result in unexpected behavior.
So, how can I run the firebase emulators:exec AFTER the emulators start as mentioned in the Firebase Docs and, can I run the JS file for the said purpose? Thanks in advance!
UPDATE:
If I run just the firebase emulators:exec setup-imtp-db.js without prior running the firebase emulators:start command, I ran into the following errors:
Note that the setup-imtp-db.js does not contain any Cloud Functions code but just a standalone JS file that populates data from another JSON file into Firestore via Admin SDK. I'm using Node.js 12.14.0 and firebase-tools 8.4.0.
According to Firebase CLI Github README.md:
emulators:exec Start the local Firebase emulators, run a test
script, then shut down the emulators.
So the scriptpath parameter isn't really meant to run a JavaScript file that pre-populates data on Firestore Emulator (and presents on its Emulator UI) for the purpose of UI Functional Test or Integrated Test. The emulators:exec will immediately shut down the emulators after finish executing the test script.
As #Doug Stevenson suggested in the comment, if a proper error handling is expected from the emulators or a new feature is required, a request or bug report can be posted on Github for firebase-tools.
While the emulators:exec command is clearly not meant for seeding a database while continuing (manual) testing, as explained in the accepted answer, I did accomplish something of the kind.
I successfully prevented my emulators:exec-invoked database seeding script from exiting with the following method:
// db-seeder.js
const admin = require('firebase-admin');
const app = admin.initializeApp({
projectId: 'demo-test'
});
const db = admin.firestore(app);
...
// Prevents the seed Node.js process from exiting,
// which in turn prevents the emulators from exiting.
process.stdin.resume();
Along with the --ui switch, this gives an experience comparable to running emulators:start, but then with a seeded database:
firebase --project demo-test emulators:exec --ui db-seeder.js
I had to use this method because I was getting credential problems when trying to seed the database with a standalone script, see details here on dev.to.
I want to start firebase emulators before Jest tests.
Execute this, but programatically:
E:\my-projct>firebase emulators:start --only firestore
i emulators: Starting emulators: firestore
i firestore: Serving ALL traffic (including WebChannel) on http://localhost:8080
! firestore: Support for WebChannel on a separate port (8081) is DEPRECATED and will go away soon. Please use port above instead.
i firestore: Emulator logging to firestore-debug.log
+ firestore: Emulator started at http://localhost:8080
i firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
+ All emulators started, it is now safe to connect.
Therefore, I need to:
Execute the command
Wait for the "All emulators started" string to appear.
How do I read the output?
I have tried the following, all it prints is a newline.
const cp = require('child_process');
const child = cp.exec('firebase emulators:start --only firestore');
child.stdout.addListener('data', data => console.log(data.toString()));
This is exactly why we have made the firebase emulators:exec command.
So let's say you use "npm run test" to run your Jest scripts, you would use:
firebase emulators:exec "npm run test"
This will:
Start the emulators and wait till they're ready
Run your script (npm run test in this case)
Wait for your script to exit
Cleanly shut down the emulators