title: TypeError: Cannot read property 'close' of undefined
body: <p>I'm new to nodejs and koa. There is this existing Koa app (running and working fine with no problem) that I am trying to write a test code using Jest. I have searched sample tests that can be possibly applied to this case but to no avail. Now, I have this error  when I run my test code <code>TypeError: Cannot read property 'close' of undefined</code>. I know there is something wrong with with my initiation of <code>server</code> variable from <code>ret</code>.</p>
  
  <p>Btw, <code>console.log(ret)</code> returns <code>undefined</code>:</p>
  
  <pre><code>[nodemon] restarting due to changes...
  [nodemon] starting `node index.js`
  undefined
  Mongoose default connection open to mongodb://localhost/giantsoftdata
  Listening to port: 3000
  </code></pre>
  
  <p>Here is the existing app that I want to test with <code>npm run test</code>:</p>
  
  <pre><code>// index.js
  const Koa = require('koa')
  const KoaBodyParser = require('koa-bodyparser')
  const KoaErrorHandler = require('@giansoft/lib-errors/lib/koaHandler')
  const PORT = 3000
  const app = new Koa()
  const router = require('./api/rest/router')()
  const dbconnect = require('./database')
  const dbstatus = require('./database/dbstatus')
  
  app
    .use(KoaErrorHandler)
    .use(dbstatus())
    .use(KoaBodyParser())
  
  const ret = dbconnect( async function () {
    app
        .use(router.routes())
        .use(router.allowedMethods())
    app.listen(PORT, () =&gt; {
        console.log(`Listening to port: ${PORT}`)
    })
    return app;
  })
  // console.log(ret)
  module.exports = ret;
  </code></pre>
  
  <p>Here is my test code:</p>
  
  <pre><code>// index.test.js
  const server = require("../index");
  const request = require("supertest");
  
  afterEach(() =&gt; {
    server.close();
  });
  
  describe('routes: /status', () =&gt; {
    test('Should respond {Status : "Ok"}.', async () =&gt; {
      const response = await request(server).get('/status');
      expect(response.status).toEqual(200);
      expect(response.type).toEqual('application/json');
    });
  });
  </code></pre>
  
  <p>Here is the <code>./database/index.js</code></p>
  
  <pre><code>const mongoose = require('mongoose')
  const dbConfig = require('../config').db
  
  const dbconnect = (callback) =&gt; {
    let connString = `mongodb://${dbConfig.host}`
    if (dbConfig.username &amp;&amp; dbConfig.password) {
      connString = `mongodb://${dbConfig.username}:${dbConfig.password}@${dbConfig.host}`
    }
  
    mongoose.connect(connString, { useNewUrlParser: true, useCreateIndex: true })
  
    mongoose.connection.on('open', function () {
      callback()
    })
  
    mongoose.connection.on('connected', function () {
      console.log('Mongoose default connection open to ' + connString)
    })
  
    mongoose.connection.on('error', function (err) {
      console.log('Mongoose default connection error: ' + err)
    })
  
    mongoose.connection.on('disconnected', function () {
      console.log('Mongoose default connection disconnected')
    })
  
    process.on('SIGINT', function () {
      mongoose.connection.close(function () {
        console.log('Mongoose default connection closed through app termination')
        process.exit(0)
      })
    })
  }
  
  module.exports = (callback) =&gt; {
    dbconnect(callback)
  }
  </code></pre>