Testing Firebase Cloud Functions: Using admin SDK with jest

Viewed 152

What is the best way to use the Firebase admin SDK in jest script to get Firestore information?

I am unable to use the admin SDK in my jest scripts. Running admin.firestore() throws:

Total timeout of API google.firestore.v1.Firestore exceeded 600000 milliseconds before any response was received.

My code:

let issueRefundWix: HttpsCallable<unknown, unknown>,

initApp = async () => {
    const serAcc: ServiceAccount = await import(GOOGLE_APPLICATION_CREDENTIALS);
    const app = initializeApp(firebaseConfig);
    const functions = getFunctions(app, "us-central1");
    const db = getFirestore(app);
    const auth = getAuth(app);

    admin.initializeApp({
        credential: admin.credential.cert(serAcc),
        storageBucket: "gs://**.appspot.com/",
    });

    return {
        app: app,
        functions: functions,
        db: db,
        auth: auth,
    };
};


beforeAll(async () => {
    const app = await initApp();
    const functions = app.functions;
    issueRefundWix = httpsCallable(functions, "issueRefundWix");
    db = admin.firestore();
    uid = await createWixUser();
    exampleSubscription.wixId = uid;
    console.log("Finished beforeAll()");
});

This set up works fine when I am running tests on the Firebase Emulator. Can I test functions the same on the emulator as when they are deployed? All tests work fine if I remove any dependencies on admin.firestore().

What is the best way to use the admin SDK to query firestore in jest tests?

Thank you

0 Answers
Related