I was following the Unit testing security rules with the Firebase Emulator Suite
There is a section where Todd creates separate functions to initialize the TestApp and AdminApp
function getFirestor(auth) {
return firebase.initializetestApp({projectId: MY_PROJECT_ID, auth: auth}).firestore
}
However, I would end up with the following error:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Eventually, I was able to determine that despite the emulator running, it wasn't connecting.
I changed from
const firebase = require('@firebase/testing')
to
const firebase = require('@firebase/rules-unit-testing');
After which I was able to modify the function to work:
const EMULATOR_PORT = 8086
function getFirestore(auth) {
const db = firebase.initializeTestApp({projectId: MY_PROJECT_ID, auth: myAuth }).firestore();
db.useEmulator("localhost", EMULATOR_PORT)
return db
}
This worked for the test app, but when I tried the same for the admin app, it does not work
function getAdminFirestore() {
const db = firebase.initializeAdminApp({projectId: MY_PROJECT_ID }).firestore();
db.useEmulator("localhost", EMULATOR_PORT)
return db
}
ERROR: TypeError: db.useEmulator is not a function
If I comment out the db.usEmulator line I get the
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
I had a firebase.json and .firebaserc in the folder above, I even tried to create another copy in the same folder as the test script, but it also didn't seem to work.