请求的模块“@firebase/rules-unit-testing”未提供名为“RulesTestContext”和“RulesTestEnvironment”的导出

j2cgzkjk  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(104)

我正在尝试使用firebase模拟器为我的安全规则创建单元测试。我遵循了一些教程,但我无法导入RulesTestContext和RulesTestEnviroment。我尝试单独导入规则,并尝试使用其他方法而不是使用RulesTestEnviroment,但它们已被弃用。
这是我的考验js:

import { getFirestore, connectFirestoreEmulator } from "firebase/firestore";
import { initializeApp } from "firebase/app";
import {equal} from 'assert';
import { getAuth } from "firebase/auth";
import * as fs from 'fs'; 
import {
 assertFails,
 assertSucceeds,
 initializeTestEnvironment,
 RulesTestEnvironment,
 RulesTestContext,
} from "@firebase/rules-unit-testing";

const MY_PROJECT_ID = 'fir-flutter-3ec5c';
const testUserId = 'userId1';

describe('Firestore security rules', () => {
 let testEnv = RulesTestEnvironment;
 let authenticatedUser = RulesTestContext;
 let unauthenticatedUser = RulesTestContext;

 beforeAll(async () => {
 testEnv = await initializeTestEnvironment({
    projectId: MY_PROJECT_ID,
    firestore: {
        rules: fs.readFileSync("firestore.rules", "utf8"),
        host: 'localhost',
        port: 8080,
    }
 });
});

beforeEach(async () => {
 await testEnv.withSecurityRulesDisabled(context => {
  const firestoreWithoutRule = context.firestore();
  return firestoreWithoutRule.collection('users').doc(testUserId).set({name: 'initial 
  user name'});
 });
 authenticatedUser = testEnv.authenticatedContext(testUserId);
 unauthenticatedUser = testEnv.unauthenticatedContext();
});

it('Only authenticated Users can create', async () => {
 const createByAuthenticatedUser =  authenticatedUser.firestore()
   .collection('users').add({name: 'user name authenticated'});

  await testing.assertSucceeds(createByAuthenticatedUser);
});

});

这是我的package.json,在test文件夹中:

{
  "name": "test",
  "version": "1.0.0",
  "description": "Unit Test",
  "main": "test.js",
  "scripts": {
    "test": "mocha --exit --timeout 100000"
  },
  "author": "jorge munin",
  "license": "ISC",
  "devDependencies": {
    "@firebase/rules-unit-testing": "^2.0.5",
    "mocha": "^10.1.0"
   },
   "type": "module"
 }
rkttyhzu

rkttyhzu1#

我只是想弄清楚firestore,但我相信你是想给变量分配类型名,这不是你想要的,只要删除它们就行了。

let testEnv;
 let authenticatedUser;
 let unauthenticatedUser;

相关问题