【问题标题】:Timeout error always occurs when tried to test firestore security rules with emulator尝试使用模拟器测试 Firestore 安全规则时总是发生超时错误
【发布时间】:2020-04-04 16:42:00
【问题描述】:

我尝试使用模拟器测试 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());
  });
});;

【问题讨论】:

  • 不应该.initializeTestApp({ projectId: RULES_PATH, auth: auth }).initializeTestApp({ projectId: PROJECT_ID, auth: auth })
  • 您是否尝试过使用 --testTimeout=&lt;number&gt; 将 Jest 的超时时间更改为 30000(30 秒)并查看它是否比预期花费的时间更长?
  • @samthecodingman 哦,你是对的。将.initializeTestApp({ projectId: RULES_PATH, auth: auth }) 修复为.initializeTestApp({ projectId: PROJECT_ID, auth: auth }) 后测试工作正常。是我粗心的错误谢谢指点。
  • 我已将我的 cmets 转换为接受的答案。很高兴你把它修好了。

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


【解决方案1】:

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

变化

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

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

应该可以解决您的问题。

如果它不能解决问题,您可以使用 --testTimeout=&lt;number of ms&gt; 将 Jest 超时更改为 5 秒以上,以便让测试有更多时间完成。

最后,为了清楚起见,考虑将createAuthApp 重命名为createFirestoreInstance,因为“创建身份验证”意味着与FirebaseAuth 类有关。

【讨论】:

  • 感谢您的回答。但是当我尝试将auth 设置为auth: ({ uid: 'alice', admin: true });firebase.initializeTestApp 时,即使在超时延长后也会发生相同的超时错误(如果身份验证信息为空,则不会发生错误)你知道吗?
  • 我无法立即提供详细帮助(下班),但您可以尝试更改 Jest 超时或将您的 await ... 呼叫包围在 try-catch 块中。
猜你喜欢
  • 2020-06-17
  • 2020-06-18
  • 2019-08-13
  • 2019-10-09
  • 1970-01-01
  • 2018-03-17
  • 2021-11-01
  • 2021-09-05
  • 2013-11-18
相关资源
最近更新 更多