How can i assert express `res.status(200).send({ hello: 'world' })` actually sent a 200 with the correct data?

How can i assert express `res.status(200).send({ hello: 'world' })` actually sent a 200 with the correct data?

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.

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.

I want to do this WITHOUT supertest as I have a helper lib with a bunch of middleware functions and I want to test those in isolation.

For a simple app like this

'use strict'

const express = require('express') const app = express() const helloWorld = require('./helloWorld')

app.get('/', helloWorld)

app.listen(5000, () => console.log('we're up!'))

With a simple handler function like this

'use strict'

function helloWorld (req, res, next) { const data = { hello: 'world' } res.status(200).send(data) }

module.exports = helloWorld

I have this test in the making

'use strict'
const helloWorld = require('./helloWorld')

describe('#helloWorld', () => { it('should return 200', () => { const req = {

}

const res = {
  status: function (code) {
    this.statusCode = code
    return this
  },
  send: function () {
    return this
  }
}

const next = () => {}

helloWorld(req, res, next)
// TODO: How to assert status was 200 and data sent was { hello: 'world' }?

}) })

How can I assert the status of 200 and the data being { hello: 'world' }?

Update This works, but idk if it's a terrible idea to do things this way.

Updated test

'use strict'

const { expect } = require('chai') const helloWorld = require('./helloWorld')

describe('#helloWorld', () => { it('should return 200', () => { 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 = () => {}

helloWorld(req, res, next)
expect(res._status).to.equal(200)
expect(res._json.hello).to.equal('world')

}) })


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to Use Express.js, Node.js and MongoDB.js

In this post, I will show you how to use Express.js, Node.js and MongoDB.js. We will be creating a very simple Node application, that will allow users to input data that they want to store in a MongoDB database. It will also show all items that have been entered into the database.

Build a REST API using Node.js, Express.js, Mongoose.js and MongoDB

Node.js, Express.js, Mongoose.js, and MongoDB is a great combination for building easy and fast REST API. You will see how fast that combination than other existing frameworks because of Node.js is a packaged compilation of Google’s V8 JavaScript engine and it works on non-blocking and event-driven I/O. Express.js is a Javascript web server that has a complete function of web development including REST API.

Secure Node.js, Express.js and PostgreSQL API using Passport.js

The comprehensive step by step tutorial on building secure Node.js, Express.js, Passport.js, and PostgreSQL Restful Web Service