firebase / firebase-functions-test

MIT License
236 stars 54 forks source link

"6 ALREADY_EXISTS: Document already exists:" even document is not exists #214

Open ASE55471 opened 1 year ago

ASE55471 commented 1 year ago

Version info

**firebase-functions-test:"^3.1.0",

**firebase-functions:"^4.4.1",

**firebase-admin:"^11.11.0",

Test case

Simply just create and delete same document in different .ts file.

Steps to reproduce

  1. Create two file below (They are doing the same thing):

t1.test.ts

import {afterEach, test} from "@jest/globals";
import firebaseFunctionsTest from "firebase-functions-test";
import * as admin from "firebase-admin";

firebaseFunctionsTest({
  projectId: "projectId",
  storageBucket: "storageBucket",
  databaseURL: "databaseURL",
}, "./key.json");

admin.initializeApp();
const defaultFirestore = admin.firestore();

// eslint-disable-next-line require-jsdoc
async function createProfileDocument() {

  // This return false
  console.log((await defaultFirestore
      .doc("profile/user")
      .get()).exists);

  // But This also return "Document already exists"
  return defaultFirestore.doc("profile/user")
      .create({});
}

afterEach(async ()=> {
  await defaultFirestore.doc("profile/user")
      .delete();
});

test("Ideal condition", async () => {
  await createProfileDocument();
});

t2.test.ts

import {afterEach, test} from "@jest/globals";
import firebaseFunctionsTest from "firebase-functions-test";
import * as admin from "firebase-admin";

firebaseFunctionsTest({
  projectId: "projectId",
  storageBucket: "storageBucket",
  databaseURL: "databaseURL",
}, "./key.json");

admin.initializeApp();
const defaultFirestore = admin.firestore();

// eslint-disable-next-line require-jsdoc
async function createProfileDocument() {

  // This return false
  console.log((await defaultFirestore
      .doc("profile/user")
      .get()).exists);

  // But This also return "Document already exists"
  return defaultFirestore.doc("profile/user")
      .create({});
}

afterEach(async ()=> {
  await defaultFirestore.doc("profile/user")
      .delete();
});

test("Ideal condition", async () => {
  await createProfileDocument();
});
  1. Then run npm run jest

Expected behavior

The tests should all passed.

Actual behavior

> test
> jest

 PASS  test/profile/t1.test.ts
  ● Console

    console.log
      false

      at test/profile/t1.test.ts:24:11

 FAIL  test/profile/t2.test.ts
  ● Console

    console.log
      false

      at test/profile/t2.test.ts:24:11

  ● Ideal condition

    6 ALREADY_EXISTS: Document already exists: projects/projectId/databases/(default)/documents/profile/user

      27 |   // But This also return "Document already exists"
      28 |   return defaultFirestore.doc("profile/user")
    > 29 |       .create({});
         |        ^
      30 | }
      31 |
      32 | afterEach(async ()=> {

      at callErrorFromStatus (node_modules/@grpc/grpc-js/src/call.ts:81:17)
      at Object.onReceiveStatus (node_modules/@grpc/grpc-js/src/client.ts:356:55)
      at Object.onReceiveStatus (node_modules/@grpc/grpc-js/src/client-interceptors.ts:454:34)
      at Object.onReceiveStatus (node_modules/@grpc/grpc-js/src/client-interceptors.ts:416:48)
      at node_modules/@grpc/grpc-js/src/resolving-call.ts:111:24
      for call at
      at ServiceClientImpl.makeUnaryRequest (node_modules/@grpc/grpc-js/src/client.ts:326:42)
      at ServiceClientImpl.<anonymous> (node_modules/@grpc/grpc-js/src/make-client.ts:189:15)
      at node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:227:29
      at node_modules/google-gax/src/normalCalls/timeout.ts:54:13
      at repeat (node_modules/google-gax/src/normalCalls/retries.ts:104:19)
      at node_modules/google-gax/src/normalCalls/retries.ts:144:7
      at OngoingCallPromise.call (node_modules/google-gax/src/call.ts:81:23)
      at NormalApiCaller.call (node_modules/google-gax/src/normalCalls/normalApiCaller.ts:43:15)
      at node_modules/google-gax/src/createApiCall.ts:118:26
      Caused by: Error: 
      at WriteBatch.commit (node_modules/@google-cloud/firestore/build/src/write-batch.js:433:23)
      at DocumentReference.create (node_modules/@google-cloud/firestore/build/src/reference.js:323:14)
      at test/profile/t2.test.ts:29:8
      at fulfilled (test/profile/t2.test.ts:28:58)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        2.995 s, estimated 3 s
Ran all test suites.
mike-4040 commented 11 months ago

Have you tried running jest with --runInBand?