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

123
Visualizações
Transaction failure while testing firebase firestore

I am trying to write some tests for the cloud functions I have written, but I am facing a problem where a transaction never succeeds and ends up in an error:

9 FAILED_PRECONDITION: the stored version (1648901730789554) does not match the required base version (0)

Problematic code is the following:

  await firebase.db().runTransaction((t) => {
    return t.get(docRef).then((doc) => {
      if (!doc.exists) {
        console.log("no exist");
      }
    });
  });

It is run several times (checked with debugger), and then the error messages is thrown...

And the firebase test env init:



export async function initialize() {
    fb.initializeTestApp({
        projectId: "my-test-project",
        auth: { uid: "alice", email: "alice@example.com" }
      });

    const testEnv = await initializeTestEnvironment({
        projectId: "demo-project-1234",
        firestore: {
            rules: fs.readFileSync("../firestore.rules", "utf8"), // Load rules from file
            // host and port can be omitted if they can be discovered from the hub.
          },        
    });
      
    const alice = testEnv.authenticatedContext(uid);
    
    const db = (alice.firestore() as unknown) as firestore.Firestore;
    firebase.db = () => db;

    return testEnv;
}

Am I doing something wrong?

Note: I currently have only one test that runs, nothing else. And firebase emulators are running, without any other app accessing it.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

After some more research, I have figured out that I was mixing @firebase/testing and firebase-admin, and the matter should not be used for unit testing backend functions according to this post.

On a side note, it seems there already is a function to create a doc iff it does not exist:

docRef.create(...).then(() => log("ok")).catch(() => log("Not ok"))
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