【问题标题】:Firebase functions:shell < test.js exampleFirebase 函数:shell < test.js 示例
【发布时间】:2021-01-12 03:53:41
【问题描述】:

Firebase Functions 有几种测试方法——一种是运行 firebase functions:shell 并手动提供对象。这对于简单的事情很好,但我想传入一个数据文件或一个测试文件,ala firebase functions:shell &lt; test.js 来自这个旧的blog post

由于不熟悉将 node 用作 REPL 或本博文中提到的特殊命令,因此我无法正常工作。

我有一个简化的触发器,看起来像:

export const onCreateUser = functions.firestore
  .document("/users/{id}")
  .onCreate(async snapshot => {
    console.log("starting...");
    const data = snapshot.data() as UserDocumentData;
    console.log(data);
    
});

使用如下所示的 test.js:

var data = { foo: 123 };
onCreateUser(data);

运行 firebase functions:shell &lt; test.js 给了我一些输出,但跳过了 starting... console.log 部分:

->firebase functions:shell < src/test.js
⚠  Your requested "node" version "10" doesn't match your global version "14"
i  functions: Loaded functions: onCreateUser
⚠  functions: The following emulators are not running, calls to these services will affect production: firestore, database, pubsub
firebase > var data = { foo: 123 };
undefined
firebase > onCreateUser(data);
'Successfully invoked function.'
firebase > ⚠  Your function was killed because it raised an unhandled error.

我需要在我的测试文件中导入该函数,还是我遗漏了其他内容?我正在寻找一种更快的方法来进行一些基本的集成测试。

这是在打字稿环境中,如果这很重要的话。

【问题讨论】:

  • 您找到发生此错误的原因了吗?当我执行 firebase functions:shell &lt; test.js 时,我遇到了同样的错误,并且我无法检查 firebase 日志,因为错误后日志被清除了。

标签: node.js typescript firebase google-cloud-firestore firebase-cli


【解决方案1】:

您将无法像这样将 TypeScript 直接发送到 Firebase CLI,尤其是如果其中包含特定于 TS 的语法,例如您在此处显示的 as UserDocumentData 部分。需要将 TypeScript 转换为纯 JavaScript,然后 nodejs 解释器才能理解它。

您可能需要进行一些挖掘以查看导致输出“您的函数已被终止,因为它引发了未处理的错误”的错误。我相信 Firebase CLI 会将调试信息发送到当前目录中的日志文件。

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多