【发布时间】:2022-07-07 19:01:14
【问题描述】:
目前正在处理一个 React/Typescript/Firebase Firestore 项目。在为从 UI 调用的某些操作/函数编写 Jest-tests 时,我遇到了以下问题:
在测试文件中,我可以使用 v9 api 设置 firestore 客户端并使其与模拟器对话
const app = initializeApp(config.firebase);
const firestore = getFirestore(app);
connectFirestoreEmulator(firestore, "localhost", 8080);
此外,我还发现了如何设置管理客户端并使其与模拟器对话
process.env.FIRESTORE_EMULATOR_HOST = "localhost:8080";
const serviceAccount = require("../../../my-key.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
...config.firebase
});
测试本身看起来像这样:
describe("createCompanyAndRating action", () => {
test("call createCompanyAndRating and make sure it creates a proper rating entity", async () => {
// omitted: set testRatingFormState and other test data that are passed as args and
// pass in the firestore db client
const {
ratingId,
companyId,
} = await createCompanyAndRating({
ratingFormState: testRatingFormState,
visitorId: testVisitorId,
firestore,
});
// verify result by fetching the rating entity from the emulator db using the admin client
const ratingPath = `companies/${companyId}/ratings/${ratingId}`;
const ratingSnap = await admin.firestore().doc(ratingPath).withConverter(ratingConverter).get();
const rating: Rating | undefined = ratingSnap.data();
// omitted: verify result with some Jest expect-statetments...
});
})
我现在的问题是 Firestore 安全规则适用,并且只有经过身份验证的用户才能在 createCompanyAndRating 函数中使用的集合中编写文档,因此调用该函数时测试已经抛出错误。
在这种情况下,我对测试安全规则本身不感兴趣。
- 有没有办法绕过测试的安全规则?
- 如果是,我必须如何设置 Firestore 客户端?
- 是否有可能在测试中以某种方式冒充用户?
另外,请注意,我不能将管理客户端传递给 createCompanyAndRating 函数,因为管理客户端 API 与我在 createCompanyAndRating 函数实现中依赖的 v9 firebase API 不同(尝试过但没有'不起作用,不仅仅是因为一些类型的错误)。
也许我的整个方法有点误入歧途,我宁愿专注于测试 createCompanyAndRating 函数的内部,在那里我做了很多可以在没有数据库交互的情况下进行测试的工厂东西。
无论如何,非常感谢任何帮助/指导。
【问题讨论】:
-
嗨@lawrence313,您能否请检查Common methods and utility functions,特别是
RulesTestEnvironment.withSecurityRulesDisabled(),看看是否有帮助。 -
感谢您确认我在正确的位置(即@firebase/rules-unit-testing)。终于弄清楚了问题所在,错过了 createCompanyAndRating 中的“等待”,所以 firestore 管理实例没有获取数据(我认为这是一个管理配置问题......)谢谢!
-
嗨@lawrence313,您能否将其发布为答案,以便它也可以帮助社区。span>
标签: reactjs testing google-cloud-firestore jestjs firebase-security