'Mocha and Chai test never starts when running an Express app

I'm trying to test an Express API with Mocha and Chai. My structure is as follows: server.js

const express = require('express');
...

const loadCredentials = () => ({
  ...
})

const application = () => {
  console.log('This is a log i should always see');
  const app = express();
  app.use('/api', authentication, authorization('@data/accounts'), router);
  ...
  return app;
};

if (require.main === module) {
  application.listen(443)
  ...
}

module.exports = { application };

test.js

const { application } = require('../server/src/server');
describe('Some async test', async () => {
  it(, async () => {
    console.log('I should really see this log!!');
    server = application();
    const res = await chai.request(server).get('/api');
    ...test stuff...
  }
}

When I lerna run test (which runs mocha ./test.js --timeout 60000) the test never executes.

lerna notice cli v3.10.7
lerna info Executing command in 1 package: "yarn run test"

However, if I disable the call to application, the test starts (and fails because server is undefined). I also tried refactoring application and passing an express() parameter to application(app) from the test, and I get the same behavior.

The test runs if I run it from WebStorm as an individual test. Needless to say the server works when I yarn run it. Any help would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source