title: async functions not executed in server when testing with chai-http
body: <p>I have the following function in my app.js</p>
  
  <pre><code>let memoryCache = require('./lib/memoryCache');
  memoryCache.init().then(() =&gt; {
     console.log("Configuration loaded on app start", JSON.stringify(memoryCache.getCache()));
  });
  
  app.use('/v1', v1);
  ...
  module.exports = app;
  </code></pre>
  
  <p><code>memorycache.init</code> is an async function where data is populated from a database</p>
  
  <pre><code>module.exports = function () {
  let cache = {};
  return {
      init: async () =&gt; {
          console.log('called this')
          cache['repairStatus'] = formatData(await getRepairStatus());
          cache['actionStatus'] = formatData(await getActionStatus());
          cache['problemFound'] = formatData(await getProblemFound());
          cache['complaintCode'] = formatData(await getComplaintCode());
          cache['productType'] = formatData(await getProductType());
          console.log('cache is', cache)
      },
      getCache: (key) =&gt; {
          if (key) return cache[key] || null;
          else return cache;
      }
  }
  </code></pre>
  
  <p>when I try to do a chai-http test, <code>memorycache.init</code> is executed after the test is run resulting in an error</p>
  
  <pre><code>let res = await chai.request(server).post(url).send(testObject)
  </code></pre>
  
  <p>Output is a 400 error after which the memoryCache is initialized.</p>
  
  <p>How to I correct this?</p>
  
  <p>Entire test:</p>
  
  <pre><code>const chai = require('chai');
  const getTestJob = require('../../lib/testutils').getTestJob;
  const chaiHttp = require('chai-http');
  const server = require('../../../app.js');
  
  chai.use(chaiHttp);
  const expect = chai.expect;
  const assert = chai.assert;
  </code></pre>
  
  <p>describe(('Api- CreateJob'), () => {
       let url = '/v1/job'
       let testSRJob = getTestJob()</p>
  
  <pre><code> before(async () =&gt; {
  
   })
  
   beforeEach(() =&gt; {
  
   })
  
   after(async () =&gt; {
   })
  
   describe('*** HAPPY CASES ***', () =&gt; {
     it('successful test', async () =&gt; {
     console.log('calling test')
     let result = await chai.request(server).post(url).set('Authorization', 'auth').send(testSRJob)
     if (result.status !== 200) {
       console.log('Status: ', result.status)
       console.log('Error: ', result.error)
       assert.fail()
     } else {
       console.log('Yippie!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
     }
    });
  })
  })
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>called this
  
  ... some logging from the api tested
  Error:  { Error: cannot POST /v1/job (400)
  status: 400,
  text: '{"message":"Create job failed","code":"E1002","errData":{}}',
  method: 'POST',
  path: '/v1/job'
  
  &gt;&gt; then failure report in the test
  
  cache is &lt;cache data&gt;
  Configuration loaded on app start &lt;cache data&gt;
  </code></pre>