Registry / testing / mock-express-response

mock-express-response

JSON →
library0.3.0jsnpmunverified

Node.js library designed to facilitate unit and integration testing of Express.js middleware and route handlers by providing a mock HTTP response object. It emulates the standard Express `response` API, including methods like `json()`, `send()`, and `render()`, allowing developers to inspect the output and behavior of their Express-dependent code without launching a full HTTP server. The package, currently at version 0.3.0, appears to be unmaintained; its last update was in 2017 and the copyright dates to 2015. It is built upon `mock-res` for core HTTP response stream mocking and is intended to be used in conjunction with `mock-express-request` for a complete request/response mock pair. A key differentiator is its direct mirroring of the Express `res` object's internal state, allowing retrieval of rendered data via methods like `_getJSON()` and `_getString()`, which simplifies assertion writing in tests. Due to its age, compatibility with modern Express.js versions (v5+) and native ESM environments is not guaranteed.

npm install mock-express-response
INSTALL
IMPORT
SIG · MOCK-EXPRESS-RESPO
M
mock-express-response
testingjavascriptv0.3.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MockExpressResponse
const MockExpressResponse = require('mock-express-response');
import MockExpressResponse from 'mock-express-response';
This package is a CommonJS module and does not support ES module imports natively. Attempting to use `import` will result in errors.

Demonstrates basic instantiation and usage of `MockExpressResponse` with common Express methods (`json`, `render`, `send`), and how to retrieve the mocked output for assertions.

const ejs = require('ejs'); const MockExpressRequest = require('mock-express-request'); const MockExpressResponse = require('mock-express-response'); // Basic usage const response = new MockExpressResponse(); // With options, including a custom render engine and a mock request object const request = new MockExpressRequest({ url: '/users/123', method: 'GET' }); const responseWithOptions = new MockExpressResponse({ render: ejs.renderFile, // Use ejs as a template engine example request: request }); // Express response methods responseWithOptions.json({ user: { id: 1, active: true } }); responseWithOptions.render('user.ejs', { user: { id: 1, name: 'Test User' } }); responseWithOptions.send('<p>User details page</p>'); // To obtain the sent json response for testing const jsonResult = responseWithOptions._getJSON(); console.log('JSON Result:', jsonResult); // To obtain the text/html response for testing const stringResult = responseWithOptions._getString(); console.log('HTML Result:', stringResult);
Debug
Known issues
gotchaThe package is effectively abandoned, with its last release (v0.3.0) in 2017 and copyright dating to 2015. It is unlikely to receive updates for bug fixes, security vulnerabilities, or compatibility with newer Node.js versions or Express.js APIs (e.g., Express v5+). Its `peerDependency` on `express@^3.0.0 || ^4.0.0` explicitly indicates lack of support for Express v5 and beyond.
fix
For new projects or those using modern Express/Node.js, consider custom mocks or more actively maintained alternatives. If used, pin the `express` peer dependency to a compatible version (v3 or v4) in your `package.json`.
affects: >=0.0.0
gotchaThis package strictly supports CommonJS (CJS) imports and does not provide native ES module (ESM) compatibility. Attempting to use `import MockExpressResponse from 'mock-express-response'` in an ESM context will fail.
fix
Ensure your project or test files are configured for CommonJS, or use dynamic `import()` for ESM projects: `const MockExpressResponse = await import('mock-express-response');` (though this might still encounter issues with the CJS module structure).
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: MockExpressResponse is not a constructor
Attempting to use `MockExpressResponse` as a class in an ES module context when it's a CommonJS export, or incorrect `require` usage.
fix
Ensure you are using `const MockExpressResponse = require('mock-express-response');` in a CommonJS file.
TypeError: response.json is not a function
The underlying Express `res` methods are not correctly mocked or inherited due to a version mismatch with the expected Express API, especially if using Express v5 or newer.
fix
Verify that your `express` peer dependency version is compatible with this library (Express v3 or v4). If using a newer Express version, this package is likely incompatible; consider manually mocking the `res` object or using a different testing approach.
Upgrade
Version history
0.3.0latest on npm
Audit
Dependencies
mock-resrequiredCore dependency providing the base mocking functionality for HTTP response streams.
expressrequiredPeer dependency; defines the API shape that this mock object emulates. Compatibility is limited to older Express versions (v3 and v4).
Agent activity
2 hits · last 30 days
node
2
Resources
mock-express-response — npm install mock-express-response · libregistry