Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!