【问题标题】:How do I tell firebase-admin to stop (when running tests)?我如何告诉 firebase-admin 停止(运行测试时)?
【发布时间】:2018-09-19 22:55:43
【问题描述】:

是的,我可以使用我的 mocha 测试运行 process.exit()--exit,但这似乎是错误的。

我正在维护的一些代码中有 firebase-admin,我正在尝试运行 mocha 测试,但根据 wtfnode:

wtfnode node_modules/.bin/_mocha --compilers coffee:coffee-script/register

Firebase 负责保持我的流程处于打开状态:

- Timers:
  - (3300000 ~ 55 min) (anonymous) @ /home/wayne/programming/universe/library/server/node_modu
les/firebase-admin/lib/firebase-app.js:147
- Intervals:
  - (45000 ~ 45 s) (anonymous) @ /home/wayne/programming/universe/library/server/node_modules/
firebase-admin/lib/database/database.js:199

非常感谢,Firebase。这令人沮丧。我可以修复数据库部分:

db = app.database();
db.goOffline();

轰隆隆。完毕。现在我只看到不会死的计时器。我该如何杀死它?我试过查看那个源代码,它把我指向了这个小地方:

FirebaseAppInternals.prototype.setTokenRefreshTimeout = function (delayInMilliseconds, numRetries) {
    var _this = this;
    this.tokenRefreshTimeout_ = setTimeout(function () {
        _this.getToken(/* forceRefresh */ true)
            .catch(function (error) {
            // Ignore the error since this might just be an intermittent failure. If we really cannot
            // refresh the token, an error will be logged once the existing token expires and we try
            // to fetch a fresh one.
            if (numRetries > 0) {
                _this.setTokenRefreshTimeout(60 * 1000, numRetries - 1);
            }
        });
    }, delayInMilliseconds);
};

不幸的是,我不知道如何获得tokenRefreshTimeout_,所以我可以cancelTimer

有没有办法告诉 firebase-admin 我已经完成了,它现在真的需要停止,还是我坚持使用 --exit

【问题讨论】:

    标签: javascript node.js firebase firebase-admin


    【解决方案1】:

    事实证明,firebase 文档真的真的模棱两可。 It says

    使此应用无法使用并释放所有相关服务的资源。

    这听起来像是“呈现您的应用程序,托管在 firebase.com 上,无法使用并释放所有相关服务的资源”

    真正的意思是“使应用程序的这个 Javascript 实例无法使用并释放资源......”

    因此,为了在 mocha 测试中关闭所有内容,您需要使用 app.delete();

    它可能看起来像这样:

    after(() => {
        var app = require('@yourstuff/FirebaseApp');
        app.delete();
    });
    

    或者它可能看起来有点不同,这取决于您的需求。

    【讨论】:

      【解决方案2】:

      @Wayne Werner 正确回答了他自己的问题。

      我正在为将 firebase admin SDK 与 firestore 一起使用的人们提供更多背景信息。这个GitHub issue 帮助回答了我的问题。

      您需要确保使用 before() 在 mocha 上下文中初始化 firebase,然后使用 after() 在 Mocha 上下文中删除应用程序

      const admin = require("firebase-admin");
      
      describe('', () => {
      
          let app;
          let db;
      
          before(async () => {
              app = await admin.initializeApp(config);
              db = app.firestore();
          });
      
          after(() => {
              app.app().delete();
          });
      
          it('....', async () => {
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        • 1970-01-01
        相关资源
        最近更新 更多