Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firestore (v9): Cannot pass compat firestore to modular functions runTransaction and writeBatch #5627

Closed
thdk opened this issue Oct 16, 2021 · 4 comments · Fixed by #5668
Closed
Assignees

Comments

@thdk
Copy link

thdk commented Oct 16, 2021

[REQUIRED] Describe your environment

  • Operating System version: ubuntu
  • Browser version: n/a
  • Firebase SDK version: 9.1.2
  • Firebase Product: firestore, rules-unit-testing

[REQUIRED] Describe the problem

If I understand the comment from issue #5550 correctly...

..., but you can pass it to any function exported from the modular SDK where a modular Firestore is accepted, such as ref().

Originally posted by @Feiyang1 in #5550 (comment)

..., then the firestore object created with:

import { initializeTestEnvironment } from "@firebase/rules-unit-testing";
import type { FirebaseFirestore } from "@firebase/firestore-types";

const firestore: FirebaseFirestore = (await initializeTestEnvironment(projectId)).unauthenticatedContext().firestore();

should be allowed to pass to modular firestore functions such as ref() but also(?) collection(), doc(), ... and runTransaction()?

import { runTransaction } from "firebase/firestore";

runTransaction(firestore, () => { ... } );

However, when passing that firestore object to runTransaction it throws the following error:

TypeError: firestore._freezeSettings is not a function

      129 |         const groupId = this.collection.newId();
      130 | 
    > 131 |         runTransaction(
          |                       ^
      132 |             this.db,
      133 |             () => {
      134 |             if (!this.rootStore.user.divisionUser)

      at configureFirestore (node_modules/@firebase/firestore/src/api/database.ts:196:30)
      at ensureFirestoreConfigured (node_modules/@firebase/firestore/src/api/database.ts:189:5)
      at runTransaction (node_modules/@firebase/firestore/src/api/transaction.ts:96:18)
@thdk thdk changed the title firestore (v9): Cannot pass compat firestore to modular function runTransaction Firestore (v9): Cannot pass compat firestore to modular function runTransaction Oct 16, 2021
@thdk thdk changed the title Firestore (v9): Cannot pass compat firestore to modular function runTransaction Firestore (v9): Cannot pass compat firestore to modular functions runTransaction and writeBatch Oct 16, 2021
@jbalidiong jbalidiong added the v9 label Oct 18, 2021
@wu-hui wu-hui self-assigned this Oct 18, 2021
@yuchenshi
Copy link
Member

FWIW, the braces for the await line seems to be in the wrong places:

- (await initializeTestEnvironment(projectId).unauthenticatedContext()).firestore();
+ (await initializeTestEnvironment(projectId)).unauthenticatedContext().firestore(); // <-- should be like this

initializeTestEnvironment(...) returns a Promise, while unauthenticatedContext(...) does not.

@thdk
Copy link
Author

thdk commented Oct 19, 2021

FWIW, the braces for the await line seems to be in the wrong places:

- (await initializeTestEnvironment(projectId).unauthenticatedContext()).firestore();
+ (await initializeTestEnvironment(projectId)).unauthenticatedContext().firestore(); // <-- should be like this

initializeTestEnvironment(...) returns a Promise, while unauthenticatedContext(...) does not.

That`s indeed a typo in the issue description :(

@thdk
Copy link
Author

thdk commented Oct 19, 2021

The relevant code to reproduce this issue is:

import { initializeTestEnvironment } from "@firebase/rules-unit-testing";
import { runTransaction } from "firebase/firestore";
const firestore = (await initializeTestEnvironment({
    projectId: "demo-test"
})).unauthenticatedContext().firestore();

runTransaction(firestore as any,
    () => Promise.resolve()
);

or wrapped in a jest test test suite:

import { initializeTestEnvironment } from "@firebase/rules-unit-testing";
import { runTransaction } from "firebase/firestore";

describe("runTransaction", () => {
    it("should not throw an error", async () => {
        const firestore = (await initializeTestEnvironment({
            projectId: "demo-test"
        })).unauthenticatedContext().firestore();

        expect(
            runTransaction(firestore as any,
                () => Promise.resolve()
            )
        ).not.toThrow();
    });
});

@wu-hui
Copy link
Contributor

wu-hui commented Oct 27, 2021

Hey, thanks for report this bug, I have put in a fix that should make this go away!

@firebase firebase locked and limited conversation to collaborators Nov 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants