node.jsexpresssupertest

TypeError: app.address is not a function even though I am exporting my server


I am getting the TypeError shown in the title while trying to run unit tests on my express routes I found 3 other questions like this, but the solutions to those do not solve my issue. I need some other possible solution.

The Error

? Compliance requests > /v1/healthcheck

    TypeError: app.address is not a function

      at Test.Object.<anonymous>.Test.serverAddress (node_modules/supertest/lib/test.js:55:18)
      at new Test (node_modules/supertest/lib/test.js:36:12)
      at Object.obj.(anonymous function) [as get] (node_modules/supertest/index.js:25:14)
      at Object.it (__tests__/routes/healthcheck.js:7:9)
          at new Promise (<anonymous>)
      at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:160:7)

  Compliance requests
    x /v1/healthcheck (3ms)

App.js

// this is the app.js I am using for my test routes

const express = require('express')
const app = express()

const translation = require('../routes/translation')
const healthcheck = require('../routes/healthcheck')

// routes
app.use(translation)
app.use(healthcheck)

const port = process.env.PORT || 3100 // set our port
let server = app.listen(port, () => {
    var host = server.address().address;
    var port = server.address().port;
    console.log('CHATS is listening at http://%s:%s', host, port);
}); 

module.export = server

healthcheck.js (tests)

// This is my test

const request = require('supertest')
const app = require('../../__test-config/app')

describe('Compliance requests', () => {
    it('/v1/healthcheck', async () => {
        const res = await request(app)
        .get('/v1/healthcheck')
        .expect(200)

        expect(res.body).toContain('CHATS IS HEALTHY')
    })
})

healthcheck.js

// This is route I am testing
const express = require('express')
const router = express.Router()

const { currentVersion } = require('../constants')

router.get(`/${currentVersion}/healthcheck`, (req, res) => {
  res.end(`<html><body><h3>${process.env.HEALTHMESSAGE}</h3></body></html>`)
})

module.exports = router

Solution

  • Small type - oh sorry - typo. :D

    module.exports instead of module.export