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')

}) })


node-js express

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

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.

Ecommerce MERN(MongoDB Express.js React.js Node.js) Redux Tailwind

Ecommerce MERN(MongoDB Express.js React.js Node.js) Redux Tailwind

Node.js Express MongoDB Tutorial

Node.js Express MongoDB Tutorial is CRUD example, I have created to showcase Express web framework, MongoDB NoSQL database, and Node.js server platform.

Node.js Express Tutorial | What Is Node.js Express

Express is a flexible Node.js web application framework which provides a wide set of features to develop both web and mobile applications. We dive into the features of this framework and also create a basic web application using Express.

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.