Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminating node process when using firebase within mocha tests #1487

Closed
klausbayrhammer opened this issue Jan 21, 2019 · 4 comments
Closed

Comments

@klausbayrhammer
Copy link

klausbayrhammer commented Jan 21, 2019

[REQUIRED] Describe your environment

  • Operating System version: MacOS 10.13.6
  • Browser version: nodejs v8.11.2
  • Firebase SDK version: firebase@^5.8.0:
  • Firebase Product: datbase

[REQUIRED] Describe the problem

Steps to reproduce:

I tried to connect to a firebase database within a mocha test, but even though I deleted the app afterwards (or even within the test) the node process is not terminating. No combination of app().delete() or database().goOffline() did work in this case.
The result is that the test runner is not able to terminate the process which means it can not be really be used within a CI environment.

It may be related to #859 but since that issue was related to auth it's possibly a different problem.

Relevant Code:

sut.js

const firebase = require('firebase/app');
require('firebase/database');

firebase.initializeApp({/* config */});
firebase.database();

module.exports = () => {
    firebase.app().delete();
};

sut.spec.js

const deleteFirebaseApp = require('./sut');
describe('firebase database within mocha tests', function () {
    it('should be able to terminate node process after deleting the firebase app', () => {
        deleteFirebaseApp();
    })
});

Run eslint sut.spec.js afterwards to invoke the tests. Also see https://github.com/klausbayrhammer/firebase-mocha-pending-connection to reproduce the problem

@google-oss-bot
Copy link
Contributor

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@Feiyang1
Copy link
Member

Thanks for providing the repro! What works for me is moving the initialization logic to beforeEach function. I don't exactly know how mocha works internally, but my guess is you need to provide the initialization code in the mocha context, and it does some cleanup for you.
Anyway I think the best practice for unit tests is using beforeEach for initialization. Hope it helps.

@klausbayrhammer
Copy link
Author

@Feiyang1 Thanks for looking into it and for providing a solid workaround - I really appreciate that.

A somewhat related issue, in case somebody stumbles upon this: If you use firebase.auth() you need to auth().signOut() so the node process can be terminated. So summarising @Feiyang1's solution - that's what worked for me

describe('', () => {
    let firebaseApp;

    before(async () => {
        firebaseApp = await firebase.initializeApp(config);
        return firebaseApp.auth().signInWithEmailAndPassword(mail, password);
    });

    after(() => {
        firebaseApp.auth().signOut();
        firebaseApp.delete();
    });

    it('....', async () => {
    });
});

@Feiyang1 would it make sense to perform things like signing out when you delete an app?

@Feiyang1
Copy link
Member

I'm glad it worked out for you. Can you please open a new issue regarding the signOut, so we can track it properly?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants