FIRESTORE (7.14.3) 内部断言失败:值必须为未定义或 Uint8Array

Ble*_*len 5 javascript node.js firebase-security flutter google-cloud-firestore

我的脚本不断引发此错误。我编写的代码用于测试 Firestore 安全规则。\n我基本上想在向其中添加 javascript 函数的同时测试我的 Firestore 安全性。可以在 firestore 控制台中编写规则并立即保存它们,但我想使用 IDE - VS 代码。

\n\n

我想知道如何修复“FIRESTORE (7.14.3) 内部断言失败:值必须是未定义或 Uint8Array”。我已经更新了我的 npm,并且还尝试了我在 Github 上找到的答案,该答案要求我先安装“npm i jest-environment-uint8array”。它不起作用。我也尝试过使用“ TextEncoder” \n例如: var uint8array = new TextEncoder("utf-8").encode("Posts");\n var string = new TextDecoder("utf-8").decode(uint8array );\n效果不太好。如果你们能彻底回答我,我将非常感激,因为我只是一个新手。

\n\n

我的测试代码是

\n\n
const { setup, teardown } = require("./helpers");\n\ndescribe("General Safety Rules", () => {\n  afterEach(async () => {\n    await teardown();\n  });\n\n  test("should deny a read to the posts collection", async () => {\n    const db = await setup();\n    const postsRef = db.collection("Posts");\n    await expect(postsRef.get()).toDeny();\n  });\n\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的助手 js 基本上在我的代码完成后进行了设置和拆卸。

\n\n
const firebase = require("@firebase/testing");\nconst fs = require("fs");\n\nmodule.exports.setup = async (auth, data) => {\n  const projectId = `rules-spec-${Date.now()}`;\n\n  const app = firebase.initializeTestApp({\n    projectId,\n    auth\n  });\n\n  const db = app.firestore();\n\n  // Apply the test rules so we can write documents\n  await firebase.loadFirestoreRules({\n    projectId,\n    rules: fs.readFileSync("firestore-test.rules", "utf8")\n  });\n\n  // write mock documents if any\n  if (data) {\n    for (const key in data) {\n      const ref = db.doc(key); // This means the key should point directly to a document\n      await ref.set(data[key]);\n    }\n  }\n\n  // Apply the actual rules for the project\n  await firebase.loadFirestoreRules({\n    projectId,\n    rules: fs.readFileSync("firestore.rules", "utf8")\n  });\n\n  return db;\n};\n\nmodule.exports.teardown = async () => {\n  // Delete all apps currently running in the firebase simulated environment\n  Promise.all(firebase.apps().map(app => app.delete()));\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误是:

\n\n
\n

开玩笑。/规格

\n
\n\n
(node:8200) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)\n FAIL  spec/collections.spec.js (10.284 s)\n  General Safety Rules\n    \xc3\x97 should deny a read to the posts collection (4598 ms)\n    \xc3\x97 should deny a write to users even when logged in (131 ms)\n\n  \xe2\x97\x8f General Safety Rules \xe2\x80\xba should deny a read to the posts collection\n\n    FIRESTORE (7.14.3) INTERNAL ASSERTION FAILED: value must be undefined or Uint8Array\n\n      at fail (node_modules/@firebase/firestore/src/util/assert.ts:39:9)\n      at hardAssert (node_modules/@firebase/firestore/src/util/assert.ts:53:5)\n      at JsonProtoSerializer.fromBytes (node_modules/@firebase/firestore/src/remote/serializer.ts:250:7)\n      at JsonProtoSerializer.fromWatchChange (node_modules/@firebase/firestore/src/remote/serializer.ts:431:32)\n      at PersistentListenStream.onMessage (node_modules/@firebase/firestore/src/remote/persistent_stream.ts:568:41)\n      at node_modules/@firebase/firestore/src/remote/persistent_stream.ts:448:21\n      at node_modules/@firebase/firestore/src/remote/persistent_stream.ts:501:18\n      at node_modules/@firebase/firestore/src/util/async_queue.ts:358:14\n\n  \xe2\x97\x8f General Safety Rules \xe2\x80\xba should deny a write to users even when logged in\n\n    FIRESTORE (7.14.3) INTERNAL ASSERTION FAILED: value must be undefined or Uint8Array\n\n      at fail (node_modules/@firebase/firestore/src/util/assert.ts:39:9)\n      at hardAssert (node_modules/@firebase/firestore/src/util/assert.ts:53:5)\n      at JsonProtoSerializer.fromBytes (node_modules/@firebase/firestore/src/remote/serializer.ts:250:7)\n      at JsonProtoSerializer.fromWatchChange (node_modules/@firebase/firestore/src/remote/serializer.ts:431:32)\n      at PersistentListenStream.onMessage (node_modules/@firebase/firestore/src/remote/persistent_stream.ts:568:41)\n      at node_modules/@firebase/firestore/src/remote/persistent_stream.ts:448:21\n      at node_modules/@firebase/firestore/src/remote/persistent_stream.ts:501:18\n      at node_modules/@firebase/firestore/src/util/async_queue.ts:358:14\n\n  console.error\n    [2020-05-20T13:13:11.851Z]  @firebase/firestore: Firestore (7.14.3): FIRESTORE (7.14.3) INTERNAL ASSERTION FAILED: value must be undefined or Uint8Array\n\n      at Logger.defaultLogHandler [as _logHandler] (node_modules/@firebase/logger/src/logger.ts:115:57)\n      at logError (node_modules/@firebase/firestore/src/util/log.ts:45:20)\n      at fail (node_modules/@firebase/firestore/src/util/assert.ts:34:3)\n      at hardAssert (node_modules/@firebase/firestore/src/util/assert.ts:53:5)\n      at JsonProtoSerializer.fromBytes (node_modules/@firebase/firestore/src/remote/serializer.ts:250:7)\n      at JsonProtoSerializer.fromWatchChange (node_modules/@firebase/firestore/src/remote/serializer.ts:431:32)\n\nTest Suites: 1 failed, 1 total\nTests:       2 failed, 2 total\nSnapshots:   0 total\nTime:        18.711 s\nRan all test suites matching /.\\\\spec/i.\n(node:8200) UnhandledPromiseRejectionWarning: FirebaseError: Caught error after test environment was torn down\n\nThe client has already been terminated.\n(node:8200) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)\n(node:8200) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.\nJest did not exit one second after the test run has completed.\n\nThis usually means that there are asynchronous operations that weren\'t stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.\n
Run Code Online (Sandbox Code Playgroud)\n

Rod*_*ers 4

看来是最新@firebase/testing@0.19.4版本和内部使用firebase@7.14.4版本之间的兼容性问题。我遇到了同样的错误,并通过临时解决方法使其正常工作。

尝试将您的@firebase/testing版本降级到^0.15.0. 很快,npm i它就对我有用了。一旦解决了当前的兼容性问题,您仍然可以稍后升级到最新版本。

一旦发生这种情况,我会立即更新我的答案。

更新:这是我在 firebase-js-sdk GitHub 页面https://github.com/firebase/firebase-js-sdk/issues/3096 上创建的问题。

似乎与测试框架 Jest 中的错误有关。该问题描述了一种不需要降级的解决方法。

一旦不再需要解决方法,我将立即更新我的答案。