title: How to test response data from Express in Jest
body: <p>I'm writing unit tests for separate middleware functions in Node/Express using Jest. </p>
  
  <p>A simple example of the middleware:</p>
  
  <pre><code>function sendSomeStuff(req, res, next) {
      try {
          const data = {'some-prop':'some-value'};
  
          res.json(data);
          next();
      } catch (err) {
          next(err);
      }
  }
  </code></pre>
  
  <p>And a sample of my test suite:</p>
  
  <pre><code>const httpMocks = require('node-mocks-http');
  const { sendSomeStuff } = require('/some/path/to/middleware');
  
  describe('sendSomeStuff', () =&gt; {
      test('should send some stuff', () =&gt; {
          const request = httpMocks.createRequest({
              method: 'GET',
              url: '/some/url'
          });
  
          let response = httpMocks.createResponse();
  
          sendSomeStuff(request, response, (err) =&gt; {
              expect(err).toBeFalsy();
  
              // How to 'capture' what is sent as JSON in the function?
          });
      });
  });
  </code></pre>
  
  <p>I have to provide a callback to populate the <code>next</code> parameter, which is called in the function. Normally, this would 'find the next matching pattern', and pass the <code>req</code> and <code>res</code> objects to that middleware. However, how can I do this in a test set-up? I need to verify the JSON from the response.</p>
  
  <p>I don't want to touch the middleware itself, it should be contained in the test environment.</p>
  
  <p>Am I missing something here? </p>