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

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

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 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