firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

SyntaxError: The requested module '@firebase/rules-unit-testing' does not provide an export named 'RulesTestEnvironment'.

Open asaadjaber opened this issue 1 year ago • 11 comments

Operating System

MacOS 14.3.

Browser Version

Firefox/122.0.

Firebase SDK Version

13.0.3

Firebase SDK Product:

Firestore

Describe your project's tooling

iOS app with Mocha v10.2.0 and node v20.9.0.

Describe the problem

I was trying to run a unit test locally to test writes to my database which is in Cloud Firestore. I wrote a test which I called 'test.js' that imports RulesTestEnvironment from the @firebase/rules-unit-testing module. The test uses an import from @firebase/rules-unit testing and another from "firebase/firestore".

import {
  assertFails,
  assertSucceeds,
  initializeTestEnvironment,
  RulesTestEnvironment,
} from "@firebase/rules-unit-testing"

When I tried to run the unit test from the command line, I ran into an error that said 'SyntaxError: The requested module '@firebase/rules-unit-testing' does not provide an export named 'RulesTestEnvironment'.

The error that appeared: Screenshot 2024-01-29 at 12 20 02

Steps and code to reproduce issue

After I started the emulator, I ran the test in this file https://github.com/asaadjaber/special-parakeet-fork/blob/cloud-firestore-unit-tests-using-mocha/Special%20Parakeet/test/test.js from the command line using npm test and I encountered the error saying 'SyntaxError: The requested module '@firebase/rules-unit-testing' does not provide an export named 'RulesTestEnvironment'.

asaadjaber avatar Jan 29 '24 11:01 asaadjaber

Thanks for reporting @asaadjaber. Have you installed the @firebase/rules-unit testing package in addition to the firebase package? I see the RulesTestEnvironment exported.

ehsannas avatar Jan 29 '24 21:01 ehsannas

Hi, no I have added the import to import RulesTestEnvironment using the sample code that was provided in the guide https://firebase.google.com/docs/firestore/security/test-rules-emulator which is here.

asaadjaber avatar Jan 30 '24 08:01 asaadjaber

Hi @ehsannas upon installing the firebase rules unit testing package and the firebase package I reran the test and I ran into the same error: Screenshot 2024-01-30 at 12 45 41

asaadjaber avatar Jan 30 '24 10:01 asaadjaber

RulesTestEnvironment is a TypeScript type, and it's not in the actual JS file (index.cjs.js) but in the typescript definitions file (index.d.ts), which would only be read by a TypeScript compiler. Since you have a minimal JS setup (using Node directly) and are not using TypeScript, you can just ignore that and not import it.

It happens to be the type returned by initializeTestEnvironment (assigned to testEnv in the docs and in your file), which doesn't matter at all if you're not using TypeScript.

hsubox76 avatar Jan 31 '24 19:01 hsubox76

Hi, I have tried commenting out the import and running the test using the initializeTestEnvironment,

let testEnv = await initializeTestEnvironment({
  projectId: "special-parakeet",
  firestore: {
    rules: fs.readFileSync("special-parakeet-firestore.rules", "utf8"),
  },
});

That resulted in this error: Screenshot 2024-02-01 at 13 52 59

asaadjaber avatar Feb 01 '24 12:02 asaadjaber

Hi @asaadjaber,

Could you post your newly updated import statement? I think that hsubox76 was suggesting that you remove only the RulesTestEnvironment element from the import, but keep the import of initializeTestEnvironment.

Thanks!

DellaBitta avatar Feb 05 '24 14:02 DellaBitta

Hi @DellaBitta , I used the code below to initialize the test environment. I can try out your suggestion though.

let testEnv = await initializeTestEnvironment({
  projectId: "special-parakeet",
  firestore: {
    rules: fs.readFileSync("special-parakeet-firestore.rules", "utf8"),
  },
});

asaadjaber avatar Feb 06 '24 13:02 asaadjaber

I checked the wrong file. I used this to import RulesTestEnvironment

import {
  assertFails,
  assertSucceeds,
  initializeTestEnvironment,
  RulesTestEnvironment,
} from "@firebase/rules-unit-testing"

asaadjaber avatar Feb 06 '24 13:02 asaadjaber

Can you update the import so that it reads:

import {
  assertFails,
  assertSucceeds,
  initializeTestEnvironment
} from "@firebase/rules-unit-testing"

and see if that does the trick? If not, then can you provide the source to a minmal reproducible test case? Thank you!

DellaBitta avatar Feb 12 '24 14:02 DellaBitta

Hi @DellaBitta , I removed the import statement for RulesTestEnvironment and then I reran the test and I got the following error: Screenshot 2024-02-14 at 12 59 59.

This is the minimum reproducible test case:

import {
  assertFails,
  assertSucceeds,
  initializeTestEnvironment,
} from "@firebase/rules-unit-testing"

let testEnv = await initializeTestEnvironment({
  projectId: "special-parakeet",
  firestore: {
    rules: fs.readFileSync("special-parakeet-firestore.rules", "utf8"),
  },
});

import { setDoc } from "firebase/firestore";

const context = testEnv.unauthenticatedContext();

test("Test simulated write by an unauthenticated user", async () => {
	await assertSucceeds(setDoc(context.firestore().collection("isFavorited"), "Sparrow"), { "name": "Sparrow", "isFavorited": true });
    // await assertSucceeds(setDoc(context.firestore(), '/isFavorited/Sparrow'), { "name": "Sparrow", "isFavorited": true });
});

asaadjaber avatar Feb 14 '24 11:02 asaadjaber

Hi @asaadjaber,

You need to also import the fs module:

import * as fs from 'fs';

That should clear up that error.

DellaBitta avatar Feb 14 '24 13:02 DellaBitta

Hey @asaadjaber. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Feb 21 '24 02:02 google-oss-bot

I reran the test and my test case now looks like this

import * as fs from 'fs';

import {
  assertFails,
  assertSucceeds,
  initializeTestEnvironment,
} from "@firebase/rules-unit-testing"

let testEnv = await initializeTestEnvironment({
  projectId: "special-parakeet",
  firestore: {
    rules: fs.readFileSync("special-parakeet-firestore.rules", "utf8"),
    host: "127.0.0.1",
    port: 8080,
  },
});

import { setDoc } from "firebase/firestore";

const context = testEnv.unauthenticatedContext();

test("Test simulated write by an unauthenticated user", async () => {
await assertSucceeds(setDoc(context.firestore().collection("isFavorited"),
"Sparrow"), { "name": "Sparrow", "isFavorited": true });
    // await assertSucceeds(setDoc(context.firestore(),
'/isFavorited/Sparrow'), { "name": "Sparrow", "isFavorited": true });
});|

The error I am getting now is: 1) Test simulated write by an unauthenticated user: FirebaseError: Expected type 'DocumentReference', but it was: a custom CollectionReference object.

On Wed, Feb 21, 2024 at 4:03 AM Google Open Source Bot < @.***> wrote:

Hey @asaadjaber https://github.com/asaadjaber. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

— Reply to this email directly, view it on GitHub https://github.com/firebase/firebase-js-sdk/issues/7987#issuecomment-1955733360, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADHMZ5HCCZHRVRW4LBDWD2DYUVIWBAVCNFSM6AAAAABCPHG4E2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNJVG4ZTGMZWGA . You are receiving this because you were mentioned.Message ID: @.***>

asaadjaber avatar Feb 21 '24 09:02 asaadjaber

@asaadjaber The error is due to passing a CollectionReference as an argument to setDoc(), which expects a DocumentReference as an argument.

Try replacing

await assertSucceeds(setDoc(context.firestore().collection("isFavorited"),
  "Sparrow"), { "name": "Sparrow", "isFavorited": true });

with

await assertSucceeds(setDoc(context.firestore().doc("isFavorited/Sparrow"),
  { "name": "Sparrow", "isFavorited": true }));

Since it appears that you have solved the originally-reported problem, I'm going to close this issue. Feel free to open a new issue if you run into bugs in Firestore. Thanks!

dconeybe avatar Feb 23 '24 19:02 dconeybe