title: Can you make Supertest wait for an Express handler to finish executing?
body: <p>I use <a href="https://github.com/visionmedia/supertest" rel="nofollow noreferrer">Supertest</a> to test my Express apps, but I'm running into a challenge when I want my handlers to do asynchronous processing <em>after</em> a request is sent. Take this code, for example:</p>
  
  <pre><code>const request = require('supertest');
  const express = require('express');
  
  const app = express();
  
  app.get('/user', async (req, res) =&gt; {
    res.status(200).json({ success: true });
    await someAsyncTaskThatHappensAfterTheResponse();
  });
  
  describe('A Simple Test', () =&gt; {
    it('should get a valid response', () =&gt; {
      return request(app)
        .get('/user')
        .expect(200)
        .then(response =&gt; {
            // Test stuff here.
        });
    });
  });
  </code></pre>
  
  <p>If the <code>someAsyncTaskThatHappensAfterTheResponse()</code> call throws an error, then the test here is subject to a race condition where it may or may not failed based on that error. Even aside from error handling, it's also difficult to check for side effects if they happen after the response is set. Imagine that you wanted to trigger database updates after sending a response. You wouldn't be able to tell from your test when you should expect that the updates have completely. Is there any way to use Supertest to wait until the handler function has finished executing?</p>