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

121
Views
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 answers
Answer question

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