尝试使用 jest 使用 v9 firestore sdk 初始化测试环境
我正在尝试设置我的测试环境来使用 firestore 测试我的安全性。我从 https://firebase.google.com/docs 复制了此代码/rules/unit-tests#before_you_run_the_emulator
let testEnv : RulesTestEnvironment;
beforeAll(async () => {
testEnv = await initializeTestEnvironment({
projectId: "demo-project-1234",
firestore: {
rules: fs.readFileSync('firestore.rules', 'utf8'),
},
});
});
但是,我收到此错误。
必须指定 firestore 模拟器的主机和端口。 (您可以使用 firebase emulators:exec './your-test-script' 包装测试脚本以启用自动发现,或通过initializeTestEnvironment({firestore: {host, port}})手动指定。
有人知道如何解决这个问题吗?
编辑
我尝试将主机和端口添加到正在运行的模拟器中,
let testEnv : RulesTestEnvironment;
beforeAll(async () => {
testEnv = await initializeTestEnvironment({
projectId: "comment-section-e9c09",
firestore: {
rules: fs.readFileSync('firestore.rules', 'utf8'),
host:'localhost',
port:8080
},
});
});
现在它似乎能够连接到我的模拟器,但是当我尝试 fx 清除数据库时,
test("sefse", () => {
testEnv.clearDatabase()
})
我收到以下错误
[UnhandledPromiseRejection:此错误源于在没有 catch 块的情况下抛出异步函数内部,或者拒绝未使用 .catch() 处理的 Promise。该承诺被拒绝,原因是“错误:必须指定数据库模拟器的主机和端口。(您可以使用 'firebase emulators:exec './your-test-script'' 包装测试脚本以启用自动发现,或者通过initializeTestEnvironment({database: {host, port}})手动指定。".] {
Im trying to set up my testing environment to test my security fules with firestore. I've copied this code from https://firebase.google.com/docs/rules/unit-tests#before_you_run_the_emulator
let testEnv : RulesTestEnvironment;
beforeAll(async () => {
testEnv = await initializeTestEnvironment({
projectId: "demo-project-1234",
firestore: {
rules: fs.readFileSync('firestore.rules', 'utf8'),
},
});
});
However, I'm getting this error.
The host and port of the firestore emulator must be specified. (You may wrap the test script with firebase emulators:exec './your-test-script' to enable automatic discovery, or specify manually via initializeTestEnvironment({firestore: {host, port}}).
Anyone know how to solve this?
EDIT
I tried adding host and port to my running emulator like so
let testEnv : RulesTestEnvironment;
beforeAll(async () => {
testEnv = await initializeTestEnvironment({
projectId: "comment-section-e9c09",
firestore: {
rules: fs.readFileSync('firestore.rules', 'utf8'),
host:'localhost',
port:8080
},
});
});
Now it seems to be able to connect to my emulator, but when I try to fx clear the database like
test("sefse", () => {
testEnv.clearDatabase()
})
I get the following error
[UnhandledPromiseRejection: 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(). The promise rejected with the reason "Error: The host and port of the database emulator must be specified. (You may wrap the test script with 'firebase emulators:exec './your-test-script'' to enable automatic discovery, or specify manually via initializeTestEnvironment({database: {host, port}}).".] {
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我给出 ua “基于摩卡”的起点:
安全.test.js:
I give u a "mocha-based" starting point:
security.test.js:
您是否像这样在 firebase.json 文件中指定了 firestore 端口和规则路径?
Did you specify the firestore port and rules path in your firebase.json file like so?