Search code examples
node.jstypescriptgoogle-cloud-functionsfirebase-tools

Connect to firebase emulator during test


Hi I'm developping an API using firebase functions and I use the emulators but I have a problem when I'm trying to access the emulators during my test to test my function to add a document in firestor it isn't working and it send data directly to firebase

Here is an example

test("return the created document with its uid", async () => {
      const res = await addDoc<any>("test", { message: "hello" });
      expect(res).toHaveProperty("uid");
    });

Instead of adding the doc to the emulators it add it directly to the database in firebase

How can I do to use the emulator instead?

EDIT: I have forgot to say that if I test by using axios to test my controller it works perfectly and use the emulator and in both case the emulators are started


Solution

  • When you are using an emulator is recommended to use a demo project[1] instead of a real project, how to do it was explained in this thread[2].

    Good luck.

    [1] https://firebase.google.com/docs/emulator-suite/connect_firestore#choose_a_firebase_project [2]How to setup a Firebase demo project