![](/img/trans.png)
[英]How do I push a value only once into firebase database (firebase-admin)?
[英]How do I tell firebase-admin to stop (when running tests)?
是的,我可以在 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
?
事實證明,firebase 文檔真的很模棱兩可。 它說
使此應用程序無法使用並釋放所有相關服務的資源。
對我來說,這聽起來像是“渲染您的應用程序,托管在 firebase.com 上,無法使用並釋放所有相關服務的資源”
它真正的意思是“使應用程序的這個 Javascript 實例無法使用並釋放資源......”
因此,為了在 mocha 測試中關閉所有內容,您需要使用app.delete();
它可能看起來像這樣:
after(() => {
var app = require('@yourstuff/FirebaseApp');
app.delete();
});
或者它可能看起來有點不同,這取決於您的需求。
@Wayne Werner 正確回答了他自己的問題。
我為將 firebase admin SDK 與 firestore 一起使用的人提供了更多背景信息。 這個GitHub 問題幫助回答了我的問題。
您需要確保使用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 () => {
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.