Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
testEnv.firestore.makeDocumentSnapshot(data, path) is not creating a new document

Setting up unit testing firebase functions (online mode).

I'm testing an onCreate() function, so I need to create a document in firestore to ensure the function is triggered and working properly. The problem I'm encountering is that testEnv.firestore.makeDocumentSnapshot(data, path) is not creating a new document. If the document already exists, I can write this data to it and trigger the onCreate() function, but if it doesn't exist I get an Error: 5 NOT_FOUND: No document to update when I run the test.

test.ts

const functions = require("firebase-functions-test");

const testEnv = functions({
  databaseURL: "https://***.firebaseio.com",
  storageBucket: "***.appspot.com",
  projectId: "***",
}, "./test-service-account.json");

import "jest";
import * as admin from "firebase-admin";


import { makeLowerCase } from "../src";

describe("makes bio lower case", () => {
  let wrapped: any;
  beforeAll(() => {
    wrapped = testEnv.wrap(makeLowerCase);
  });

  test("it converts the bio to lowercase", async () => {

    const path = "/animals/giraffe";
    const data = {bio: "GIRAFFE"};

    const snap = testEnv.firestore.makeDocumentSnapshot(data, path);

    await wrapped(snap)

    const after = await admin.firestore().doc(path).get();

    expect(after?.data()?.bio).toBe("giraffe");

  });
});

makeLowerCase.ts

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

export const makeLowerCase = functions.firestore
  .document("animals/{animalId}")
  .onCreate((snap, context) => {
    const data = snap.data();
    const bio = data.bio.toLowerCase();

    return admin.firestore().doc(`animals/${snap.id}`).update({bio});
  });

I can fix this in makeLowerCase.ts by returning:

admin.firestore().doc(`animals/${snap.id}`).set({bio}, {merge: true});

Or by creating the doc with admin in the test:

await admin.firestore().doc(path).set(data);

But the testEnv.firestore.makeDocumentSnapshot(data, path); should be creating a document I think, no?

Is this is bug or expected behaviour from firebase-functions-test"?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

makeDocumentSnapshot(data, path) won't create an actual document, it only spoofs a QueryDocumentSnapshot object without interacting with any actual database - think of it as "make DocumentSnapshot object".

While your Cloud Function can rightly assume that the document does in fact exist because that's how it is triggered, you'd have to write a document to the database for it to act upon if you wish to keep using update(...) instead of set(..., { merge: true }).

So you'll need to at least add:

await admin.firestore().doc(path).set(data);

and then you can use either of these:

const snap = testEnv.firestore.makeDocumentSnapshot(data, path);
// OR
const snap = await admin.firestore().doc(path).get();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!