-
Notifications
You must be signed in to change notification settings - Fork 974
Description
- Operating System version: Linux Mint 20.3
- Browser version: node 16.14.0
- Firebase SDK version:
- "firebase": "^9.6.9",
- "@firebase/rules-unit-testing": "^2.0.2",
- Firebase Product: firestore
I am trying to update my unit tests to work with firebase v9. For firebase v8 I had used mocks and dependency injection, but this is harder with v9 as now every method (e.g. collection, getDoc, setDoc) is separated. In comes this guide: https://firebase.google.com/docs/rules/unit-tests I have it partially working with this new rules unit testing library, but it seems to break with a firebase v9 call to firestore collection()
with this error:
Rejected promise returned by test. Reason:
FirebaseError {
code: 'invalid-argument',
customData: undefined,
toString: Function {},
message: 'Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore',
}
Steps to reproduce:
Relevant Code:
import fs from "fs";
import {
assertSucceeds,
initializeTestEnvironment,
RulesTestEnvironment,
} from "@firebase/rules-unit-testing";
import { collection } from "firebase/firestore";
const EMAILS_BY_PLAN = {
0: "[email protected]"
};
const fsTestEnv = await initializeTestEnvironment({
projectId: "demo-lipsurf",
firestore: {
host: "0.0.0.0",
port: 8080,
rules: fs.readFileSync("../backend/firebase/firestore.rules", "utf8"),
},
});
await fsTestEnv.withSecurityRulesDisabled(async (context) => {
const db = context.firestore();
for (const plan in EMAILS_BY_PLAN) {
const email = EMAILS_BY_PLAN[plan];
const doc = db.collection("users").doc(email);
await assertSucceeds(
doc.set({
plan: +plan,
})
);
}
});
const db = fsTestEnv
.authenticatedContext(EMAILS_BY_PLAN[FREE_PLAN])
.firestore();
// This is the line that throws the error
collection(db, "users")
// this works
// db.collection("users")
The reason I'm not simply switching to the v8 way of doing things is because that's the way my code works everywhere since I've migrated to v9. Perhaps I have the wrong approach all-together? Maybe I need some kind of mocking library to handle calls to firestore? Worth noting that I'm using the v9 compatibility library (because of firebase-ui).