Jest/Supertest errors with TypeError: app.address is not a function

Firstly, I have tried the answer here - didn't help to resolve, the accepted answer didn't resolve my issue.

I am trying to test my API endpoints with Jest/Supertest. Starting with a simple /test endpoint. However when I run the tests, I get: TypeError: app.address is not a function.

app.js:

...
// Set server (default to 3000)
  app.set('port', process.env.PORT || 3000);
  // Start server
  const server = http.listen(app.get('port'), () => {
    logger.info(`Worker ${process.pid} running on ${app.get('port')} in ${app.get('env')} mode`);
  });

  module.exports = server;
...

app.test.js:

const server = require('./app');
const supertest = require('supertest');
const request = supertest(server);

it('Gets the test endpoint', async (done) => {
  // Sends GET Request to /test endpoint
  const res = await request.get('/test');
  done();
});

Test run output:

FAIL  ./app.test.js
  ✕ Gets the test endpoint (24 ms)

  ● Gets the test endpoint

    TypeError: app.address is not a function

      14 | it('Gets the test endpoint', async (done) => {
      15 |   // Sends GET Request to /test endpoint
    > 16 |   const res = await request.get('/test');
         |                             ^
      17 |
      18 |   // ...
      19 |   done();

Any input would be welcome, thanks.

1 answer