How to Mock Express in Jest: A Senior Developer's Guide
If you’ve ever written unit tests for a Node.js application using Express, you’ve likely faced the challenge of mocking the express
library. Whether you're testing middleware, routes, or controllers, isolating dependencies like express
is crucial to ensure your tests are fast, deterministic, and truly unit-level.
In this guide, I'll show you how to mock the Express library using Jest — the most popular testing framework for JavaScript and TypeScript projects.
TL;DR: Learn how to mock
express
with Jest to unit test your route handlers and middleware without spinning up an actual server.
Why Mock Express in Unit Tests?
When unit testing your code, you want to test your logic — not whether Express itself is working. By mocking express
, you avoid:
- Starting a real server
- Hitting actual network layers
- Tightly coupling your tests to Express internals
This also allows you to:
✅ Focus on business logic ✅ Simulate edge cases easily ✅ Improve test speed and reliability
Prerequisites
Before we dive into mocking Express, make sure your environment is ready:
npm install --save-dev jest
npm install express
Example Use Case: Testing a Route Handler
Here’s a basic Express route in userRoutes.js
:
const express = require('express');
const router = express.Router();
router.get('/users', (req, res) => {
res.json({ message: 'List of users' });
});
module.exports = router;
Let’s say you're importing and using this router in your app.js
. Now, you want to test a function that mounts this router, but without actually spinning up a server.
Step-by-Step: How to Mock express.Router()
in Jest
Let’s walk through mocking the express.Router
functionality.
✅ 1. Create the Jest Mock
Create a manual mock of express
in __mocks__/express.js
:
const routerMock = {
get: jest.fn(),
post: jest.fn(),
put: jest.fn(),
delete: jest.fn(),
};
module.exports = {
Router: () => routerMock,
};
✅ Pro Tip: You can expand the mock to cover any HTTP methods you use in your app.
✅ 2. Tell Jest to Use Your Mock
In your test file (e.g., userRoutes.test.js
):
jest.mock('express');
const express = require('express');
const userRoutes = require('./userRoutes');
describe('User Routes', () => {
it('should register the GET /users route', () => {
const router = express.Router();
// Import the router, which triggers the route definition
require('./userRoutes');
expect(router.get).toHaveBeenCalledWith('/users', expect.any(Function));
});
});
This verifies that your route was correctly registered on the mocked router. You can go further by invoking the handler function directly and asserting its output.
Mocking req
and res
for Middleware/Controller Testing
You can also unit test your route handlers without Express at all by mocking req
and res
:
const handler = (req, res) => {
res.json({ status: 'OK' });
};
test('should return OK status', () => {
const req = {};
const res = {
json: jest.fn(),
};
handler(req, res);
expect(res.json).toHaveBeenCalledWith({ status: 'OK' });
});
Summary: Key Takeaways
✅ Mock express.Router
manually with Jest to intercept route registration
✅ Test handlers in isolation by mocking req
and res
✅ Never start a real server in unit tests — that’s what integration tests are for!
Final Thoughts
Mocking Express in Jest isn’t as scary as it seems. With a few clean mocks, you can decouple your tests from Express internals and focus on testing the logic that really matters.
If you’re building a robust backend with Node.js and Express, mocking Express is a must-have technique in your TDD toolkit.