Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda