javascript - 尝试使用模拟器测试 Firestore 安全规则时总是出现超时错误

标签 javascript firebase google-cloud-firestore firebase-security firebase-tools

我尝试使用模拟器测试 firestore 安全规则,但总是出现超时错误... 如果你有同样的现象或者有解决办法请告诉我。

测试结果

启动模拟器

% firebase serve --only firestore

运行测试

% yarn test
yarn run v1.19.2
$ jest
 FAIL  tests/firestore.test.ts (7.123s)
  Firestore Security Rule
    ✕ sample1 (5044ms)

  ● Firestore Security Rule › sample1

    : Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error:

      30 |   });
      31 |
    > 32 |   test("sample1", async () => {
         |   ^
      33 |     const db = createAuthApp();
      34 |     const user = usersRef(db).doc("test");
      35 |     await firebase.assertSucceeds(user.set({ name: "John" }));

      at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Suite.<anonymous> (tests/firestore.test.ts:32:3)
      at Object.<anonymous> (tests/firestore.test.ts:16:1)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        8.038s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

环境

※由于示例,Firestore 安全规则允许一切

■firestore.test.ts

import * as firebase from "@firebase/testing";
import * as fs from "fs";

const PROJECT_ID = "firestore-rule-test";
const RULES_PATH = "firestore.rules";

// Create Firestore app with auth
const createAuthApp = (auth?: object): firebase.firestore.Firestore => {
  return firebase
    .initializeTestApp({ projectId: RULES_PATH, auth: auth })
    .firestore();
};

const usersRef = (db: firebase.firestore.Firestore) => db.collection("user");

describe("Firestore Security Rule", () => {
  beforeAll(async () => {
    await firebase.loadFirestoreRules({
      projectId: PROJECT_ID,
      rules: fs.readFileSync(RULES_PATH, "utf8")
    });
  });

  afterEach(async () => {
    await firebase.clearFirestoreData({ projectId: PROJECT_ID });
  });

  afterAll(async () => {
    await Promise.all(firebase.apps().map(app => app.delete()));
  });

  test("sample1", async () => {
    const db = createAuthApp();
    const user = usersRef(db).doc("test");
    await firebase.assertSucceeds(user.set({ name: "John" }));
    await firebase.assertSucceeds(user.get());
  });
});;

最佳答案

在你的createAuthApp()函数,您正在初始化项目 ID 为 RULES_PATH 的测试应用程序但在您的测试中,您正在使用 PROJECT_ID 的项目 ID 加载安全规则.

改变

.initializeTestApp({ projectId: RULES_PATH, auth: auth })

.initializeTestApp({ projectId: PROJECT_ID, auth: auth })

应该可以解决您的问题。

如果它不能解决问题,您可以使用 --testTimeout=<number of ms> 将 Jest 超时更改为超过 5 秒。给测试更多的时间来完成。

最后,为了清楚起见,考虑重命名 createAuthAppcreateFirestoreInstance因为“创建 Auth”意味着与 FirebaseAuth 有关类。

关于javascript - 尝试使用模拟器测试 Firestore 安全规则时总是出现超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59285120/

相关文章:

javascript - 使用 ngrx 效果捕获 Firebase Auth 错误

带有 Firebase RecyclerView 的 Android 搜索 View 不显示数据

ios - 从 Firebase 的 URL 加载图像时,CollectionsView 性能不稳定

ios - 检查 Firestore 查询 (whereField isEqualTo) 是否未找到任何文档

javascript - 如何为我的案例制作最好的 firestore 模式?

javascript - Firestore云功能。https.onRequest Request.url.split ("/")不起作用

javascript - React Serviceworker 注册

javascript - 将模型中没有的数据返回到 mvc Controller

javascript - 使用默认值初始化数组

javascript - 按清除按钮后计算器键盘输入显示困惑