Usage with Jest and beforeAll() / afterAll()
I'm trying to plumb in firebase-server to use with my Jest tests for a node app that runs some batch jobs and timer processes for my web application.
In writing tests for my various modules, I've run into a problem with firebase-server.
I'm trying to start a firebase-server fresh with each test suite (meaning once per file in my __test__ directory), using jest's beforeAll and afterAll hooks (link for reference).
I must be doing something wrong - my beforeAll call looks like this:
let server;
let database;
beforeAll(() => {
server = new FirebaseServer(5000, 'my.local.url', MockDatabase);
const config = {
databaseURL: Constants.TEST.FIREBASE_TEST_SERVER_URL,
credential: admin.credential.applicationDefault(),
};
admin.initializeApp(config);
database = admin.database();
});
and afterAll looks like this:
afterAll(() => {
if (server) {
server.close();
server = null;
}
});
However, when I run my tests, it appears that one test file's firebase-server isn't closing fully before the next file's server tries to open, and I get an EADDRINUSE error from node:
Error: listen EADDRINUSE 0.0.0.0:5000
at Object.exports._errnoException (util.js:1012:11)
at exports._exceptionWithHostPort (util.js:1035:20)
at Server._listen2 (net.js:1252:14)
at listen (net.js:1288:10)
at net.js:1398:9
at _combinedTickCallback (internal/process/next_tick.js:77:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
Jest's beforeAll/afterall functions are configured to work with promises, but from what I can tell, there's not an easy way to do that with firebase-server's callback syntax. Is there a simple solution to this that I'm missing?
Thanks!
I have also the same problem on MBP, but the test on my friend's Arch-Linux works.
Still, can't figure out where's the problem.
Interesting to hear it works on linux - just as in my snippet above, @leedan77?
@mbifulco
We use 'detect-port' to check if the port is occupied.
Like this:
import firebase from 'firebase';
import FirebaseServer from 'firebase-server';
import detect from 'detect-port';
export async function startFirebaseTestServer() {
let server;
const port = await detect(5000);
if (port === 5000) {
server = new FirebaseServer(5000, '127.0.0.1', {});
}
firebase.initializeApp({ databaseURL: 'ws://127.0.1:5000' });
const dbRef = firebase.app().database().ref();
return { server, dbRef };
}
and in Jest you can do like this:
let dbRef;
beforeAll(async () => {
({ dbRef } = await startFirebaseTestServer());
});
No need to close server in afterAll.
Ah yeah, that's not a bad workaround - depending on how many tests you have. I guess it would take up a good bit of memory after a while, too. detect-port is a good tip - thanks for that!
@urish in the meantime, do you have any tips for using this on macOS with jest?
Actually, I don't use neither macOS nor jest :-)
On Feb 12, 2017 5:02 PM, "Mike Bifulco" [email protected] wrote:
Ah yeah, that's not a bad workaround - depending on how many tests you have. I guess it would take up a good bit of memory after a while, too. detect-port is a good tip - thanks for that!
@urish https://github.com/urish in the meantime, do you have any tips for using this on macOS with jest?
— You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/urish/firebase-server/issues/79#issuecomment-279224125, or mute the thread https://github.com/notifications/unsubscribe-auth/AA2dnvTocMCvJF1fW-GUc9LQH0pQmxpyks5rbx79gaJpZM4L3JYO .
Note: I just found another trick - if you specify port as null, the server will automatically choose an available port. You can then get the port number through firebaseServer._wss._server.address().port,
though it is currently not a supported public API. That's how I do it in my own tests.
If anyone is willing to send a PR that will add a getPort() that would be awesome.
Not 100% the same as starting a new server before each suite, but you can use Jest's globalSetup and globalTeardown scripts to start the Firebase Server once and close it off afterwards.
// getMockFirebaseServer.js
const FirebaseServer = require('firebase-server');
const testData = require('./db-for-tests.json');
let server;
module.exports = async () => {
if (server) {
return server;
}
server = await new FirebaseServer(5000, 'localhost', testData);
return server;
};
// jest-global-setup.js
const getMockFirebaseServer = require('./getMockFirebaseServer');
module.exports = async () => getMockFirebaseServer();
// jest-global-teardown.js
const getMockFirebaseServer = require('./getMockFirebaseServer');
module.exports = async () => {
const server = await getMockFirebaseServer();
return server.close();
};
And then where ever you have your Jest config, set:
{
globalSetup: './path/to/jest-global-setup.js',
globalTeardown: './path/to/jest-global-teardown.js'
};