How to mock Firestore in Firebase Api V9?

Viewed 131

I am migrating to Angular 12, Angular Fire 7 and Firebase 9, but I am failing to mock firestore.

I've migrated all my code to the tree shakeable format: ie:

import { doc, getDoc } from "firebase/firestore";

class ProfileDao {

  constructor(protected firestore: Firestore) {}

  get(id: string) {
    const docRef = doc(db, "profile", id);
    return getDoc(docRef)
  }
}

But now, my tests are failing w/ the message: Error: AngularFireModule has not been provided at getScheduler

Here is an example:

fdescribe("ProfileDao", () => {
  let firestore: Firestore;
  let profileDao: ProfileDao;
  let uid: string;

  beforeEach(() => {
    firestore = jasmine.createSpyObj("Firestore", ["x"]); // I know it is wrong here.
    uid = faker.datatype.uuid();
    profileDao = new ProfileDao(firestore);
  });

  it ("calls firestore with the document with the given id", async () => {
    profileDao.get(uid); // The error is thrown here (because getDoc tries to a global something something from my mock

    expect(firestore.x).toHaveBeenCalled();
  });

Firestore snipet:

function getSchedulers() {
    const schedulers = globalThis.ɵAngularFireScheduler;
    if (!schedulers) {
        throw new Error('AngularFireModule has not been provided');
    }
    return schedulers;
}

So what is the correct way to mock firestore in v9?

0 Answers
Related