Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda