title: How can i assert express `res.status(200).send({ hello: 'world' })` actually sent a 200 with the correct data?
body: <p>I want to learn how to properly unit test an express endpoint, specifically just the handler code, and assert the correct status and data in the response.</p>
  
  <p>I want to do this <strong>WITHOUT</strong> supertest as I have a helper lib with a bunch of middleware functions and I want to test those in isolation.</p>
  
  <p>For a simple app like this</p>
  
  <pre><code>'use strict'
  
  const express = require('express')
  const app = express()
  const helloWorld = require('./helloWorld')
  
  app.get('/', helloWorld)
  
  app.listen(5000, () =&gt; console.log('we\'re up!'))
  </code></pre>
  
  <p>With a simple handler function like this</p>
  
  <pre><code>'use strict'
  
  function helloWorld (req, res, next) {
    const data = {
      hello: 'world'
    }
    res.status(200).send(data)
  }
  
  module.exports = helloWorld
  </code></pre>
  
  <p>I have this test in the making</p>
  
  <pre><code>'use strict'
  const helloWorld = require('./helloWorld')
  
  describe('#helloWorld', () =&gt; {
    it('should return 200', () =&gt; {
      const req = {
  
      }
  
      const res = {
        status: function (code) {
          this.statusCode = code
          return this
        },
        send: function () {
          return this
        }
      }
  
      const next = () =&gt; {}
  
      helloWorld(req, res, next)
      // TODO: How to assert status was 200 and data sent was { hello: 'world' }?
    })
  })
  </code></pre>
  
  <p>How can I assert the status of 200 and the data being <code>{ hello: 'world' }</code>?</p>
  
  <p><strong>Update</strong>
  This works, but idk if it's a terrible idea to do things this way.</p>
  
  <p>Updated test</p>
  
  <pre><code>'use strict'
  
  const { expect } = require('chai')
  const helloWorld = require('./helloWorld')
  
  describe('#helloWorld', () =&gt; {
    it('should return 200', () =&gt; {
      const req = {
  
      }
  
      const res = {
        _status: null,
        _json: null,
        status: function (code) {
          this._status = code
          return this
        },
        send: function (json) {
          this._json = json
          return this
        }
      }
  
      const next = () =&gt; {}
  
      helloWorld(req, res, next)
      expect(res._status).to.equal(200)
      expect(res._json.hello).to.equal('world')
    })
  })
  </code></pre>