简体   繁体   中英

How do I tell firebase-admin to stop (when running tests)?

Yes, I can run process.exit() or --exit with my mocha tests, but that seems wrong.

I've got firebase-admin in some code that I'm maintaining and I'm trying to run mocha tests, but according to wtfnode:

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

Firebase is responsible for holding my process open:

- 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

Thanks a lot, Firebase. This is frustrating. I can fix the database part:

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

Boom. Done. Now I just see the timer that won't die.How do I kill that? I've tried looking at that source code, which points me to this little spot:

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);
};

Unfortunately, I'm not sure how to get that tokenRefreshTimeout_ so I can cancelTimer .

Is there a way to tell firebase-admin that I'm done and it really needs to stop now, or am I stuck with --exit ?

Turns out the firebase documentation is just really really ambiguous. It says

Renders this app unusable and frees the resources of all associated services.

What that sounded like to me is "Renders your app, hosted on firebase.com, unusable and frees the resources of all associated services"

What it really means is "makes this Javascript instance of the app unusuable and frees up the resources..."

So in order to get everything to shut down in a mocha test you need to use app.delete();

It might look something like this:

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

Or it might look a little different, depending on what your needs are.

@Wayne Werner correctly answered his own question.

I am giving a little more context for people who use the firebase admin SDK with firestore. This GitHub issue helped answer my question.

You need to make sure you initialize firebase within the mocha context with before() and then also deleting the app within the Mocha context with after()

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 () => {
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM