Javascript FIRESTORE(7.14.3)内部断言失败:值必须为未定义或Uint8Array
我的脚本不断出现此错误。我编写的代码用于测试Firestore安全规则。 我基本上是想在向firestore添加javascript函数的同时测试firestore的安全性。可以在firestore控制台中编写规则并保存它们,但我想使用IDE-VS代码 我想知道如何修复“FIRESTORE(7.14.3)内部断言失败:值必须是未定义的或Uint8Array”。我已经更新了我的npm,还尝试了在Github上找到的答案,上面说我要安装第一个“npm I jest-environment-uint8array”。它不起作用。我也尝试过使用“TextEncoder” 比如:var uint8array=newtextcoder(“utf-8”).encode(“Posts”); var字符串=新的文本解码器(“utf-8”)。解码(uint8array); 但效果并不理想。如果你们能彻底回答我,我会非常感激,因为我只是个新手 我的测试代码是Javascript FIRESTORE(7.14.3)内部断言失败:值必须为未定义或Uint8Array,javascript,node.js,flutter,google-cloud-firestore,firebase-security,Javascript,Node.js,Flutter,Google Cloud Firestore,Firebase Security,我的脚本不断出现此错误。我编写的代码用于测试Firestore安全规则。 我基本上是想在向firestore添加javascript函数的同时测试firestore的安全性。可以在firestore控制台中编写规则并保存它们,但我想使用IDE-VS代码 我想知道如何修复“FIRESTORE(7.14.3)内部断言失败:值必须是未定义的或Uint8Array”。我已经更新了我的npm,还尝试了在Github上找到的答案,上面说我要安装第一个“npm I jest-environment-uint8
const { setup, teardown } = require("./helpers");
describe("General Safety Rules", () => {
afterEach(async () => {
await teardown();
});
test("should deny a read to the posts collection", async () => {
const db = await setup();
const postsRef = db.collection("Posts");
await expect(postsRef.get()).toDeny();
});
});
我的助手js基本上完成了设置和代码完成后的拆卸
const firebase = require("@firebase/testing");
const fs = require("fs");
module.exports.setup = async (auth, data) => {
const projectId = `rules-spec-${Date.now()}`;
const app = firebase.initializeTestApp({
projectId,
auth
});
const db = app.firestore();
// Apply the test rules so we can write documents
await firebase.loadFirestoreRules({
projectId,
rules: fs.readFileSync("firestore-test.rules", "utf8")
});
// write mock documents if any
if (data) {
for (const key in data) {
const ref = db.doc(key); // This means the key should point directly to a document
await ref.set(data[key]);
}
}
// Apply the actual rules for the project
await firebase.loadFirestoreRules({
projectId,
rules: fs.readFileSync("firestore.rules", "utf8")
});
return db;
};
module.exports.teardown = async () => {
// Delete all apps currently running in the firebase simulated environment
Promise.all(firebase.apps().map(app => app.delete()));
};
错误是:
开玩笑
这似乎是最新的
@firebase之间的兼容性问题/testing@0.19.4
版本和内部使用的firebase@7.14.4
版本。我也犯了同样的错误,并用临时解决方法解决了它
尝试将@firebase/testing
版本降级为^0.15.0
。经过一次快速的npm我
它对我起了作用。只要解决了当前的兼容性问题,您仍然可以稍后升级到最新版本
我会尽快更新我的答案
更新:
这是我在firebase js sdk GitHub页面上创建的问题
似乎与测试框架中的一个bug有关。该问题描述了一种不需要降级的解决方法
一旦不再需要解决方法,我将立即更新我的答案。以下是Firebase问题:
spec/collections.spec.js
中:
/**
* @jest-environment node
*/
describe("my rules", () => {
// ...
});
基本问题是jsdom
与firebase
库使用的内部断言之间的不兼容,记录如下:
Tony O'Hagan()指出的解决方法允许在JSDOM环境中运行测试,但解决方案相当复杂。如果您只测试Firestore安全规则,那么就不需要JSDOM
更新:(2021年5月)将Jest的默认行为更改为在节点环境中运行,而不是在JSDOM中运行是的,降级可以工作,不知道如何工作,但需要注意的是,这一问题现在已经得到了正确解决(至少我认为这是他们的预期解决方案),方法是安装“Jest环境节点”并添加测试环境:“节点”jest.config.js
/**
* @jest-environment node
*/
describe("my rules", () => {
// ...
});