How to mock express middleware in Jest

Learn how to mock Express middleware functions in Jest tests using jest.mock.

Let's say we have hello-world express app, which uses middleware to log time with every request.

// app.js File
const express = require("express");
const app = express();
const logTime = require("./middleware");

app.use(logTime); // <=== Log time

app.get("/", (req, res) => {
  res.send("Hello World!");
});

module.exports = app;
// middleware.js
const logTime = (req, res, next) => {
  console.log("Time:", new Date().toLocaleString());
  next();
};

module.exports = logTime;
// server.js File
const port = 8080;
const app = require("./app");

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

When app is run and get request made on http:localhost:8080, Time would be logged in terminal.

Mocking middleware

// app.test.js
const request = require("supertest");
const app = require("./app");
const logTime = require("./middleware");

jest.mock("./middleware", () =>
  jest.fn((_, __, next) => {
    console.log("Log from mock function");
    return next();
  }),
);

test("should mock middleware func", async () => {
  await request(app).get("/").expect(200);
  expect(logTime).toBeCalledTimes(1);
});

After the test is run, time won't be logged, but we'd see Log from mock function in terminal:

  console.log
    Log from mock function

      at log (app.test.js:7:11)

 PASS  ./app.test.js
  ✓ should mock middleware func (31 ms)