Boris Grunwald
Boris Grunwald

Reputation: 2712

Trying to initialize test environment with v9 firestore sdk using jest

Im trying to set up my testing environment to test my security fules with firestore. I've copied this code from https://firebase.google.com/docs/rules/unit-tests#before_you_run_the_emulator

let testEnv : RulesTestEnvironment;

beforeAll(async () => {

    testEnv = await initializeTestEnvironment({
        projectId: "demo-project-1234",
        firestore: {
            rules: fs.readFileSync('firestore.rules', 'utf8'),
        },
    });

});

However, I'm getting this error.

The host and port of the firestore emulator must be specified. (You may wrap the test script with firebase emulators:exec './your-test-script' to enable automatic discovery, or specify manually via initializeTestEnvironment({firestore: {host, port}}).

Anyone know how to solve this?

EDIT

I tried adding host and port to my running emulator like so

let testEnv : RulesTestEnvironment;

beforeAll(async () => {

    testEnv = await initializeTestEnvironment({
        projectId: "comment-section-e9c09",
        firestore: {
            rules: fs.readFileSync('firestore.rules', 'utf8'),
            host:'localhost',
            port:8080
        },
    });

});

Now it seems to be able to connect to my emulator, but when I try to fx clear the database like

test("sefse", () => {
    testEnv.clearDatabase()
})

I get the following error

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Error: The host and port of the database emulator must be specified. (You may wrap the test script with 'firebase emulators:exec './your-test-script'' to enable automatic discovery, or specify manually via initializeTestEnvironment({database: {host, port}}).".] {

Upvotes: 5

Views: 1842

Answers (2)

Nick Gray
Nick Gray

Reputation: 1

Did you specify the firestore port and rules path in your firebase.json file like so?

"emulators": {
  "firestore": {
    "port": 8080
  }
},
"firestore": {
  "rules": "./rules/firestore.rules"
}

Upvotes: 0

commander
commander

Reputation: 41

I give u a "mocha-based" starting point: security.test.js:

import { readFileSync } from 'fs';
import { assertFails, assertSucceeds, initializeTestEnvironment } from "@firebase/rules-unit-testing";
import { doc, getDoc, setDoc } from "firebase/firestore";

let testEnv;
let unauthedDb;

describe("general database behaviour", () => {

before(async () => {
  testEnv = await initializeTestEnvironment({
    projectId: "demo-project-1234",
    firestore: {
      rules: readFileSync("firestore.rules", "utf8"),
      host: "127.0.0.1",
      port: "8080"
    },
  });
  unauthedDb = testEnv.unauthenticatedContext().firestore();
});

after(async () => {
  await testEnv.cleanup();
});

  it("should let read anyone the database", async () => {
    await testEnv.withSecurityRulesDisabled(async (context) => {
      await setDoc(doc(context.firestore(), 'data/foobar'), { foo: 'bar' });
    }); 
    await assertSucceeds(getDoc(doc(unauthedDb, 'data/foobar')))
  })
  it("should not allow writing the database", async () => {
    await assertFails(setDoc(doc(unauthedDb, '/data/foobar'), { something: "something" }))
  })
})

Upvotes: 4

Related Questions