Registry / testing / mock-express-request

mock-express-request

JSON →
library0.2.2jsnpmunverified

Mock Express Request is a Node.js library designed to create mock HTTP request objects for unit testing Express.js applications. It is based on the `mock-req` package and aims to provide an instance with properties and methods similar to a real Express HTTP request. The package is currently at version 0.2.2 and appears to be abandoned, with no significant updates or maintenance activity in approximately nine years. Due to its age, it lacks modern features such as native ES Module support and deep integration with contemporary testing frameworks like Jest, making it less suitable for current Node.js and Express.js projects. Developers are generally advised to consider more actively maintained alternatives like `node-mocks-http` or `@jest-mock/express` for robust testing in modern environments.

npm install mock-express-request
INSTALL
IMPORT
SIG · MOCK-EXPRESS-REQUE
M
mock-express-request
testingjavascriptv0.2.2
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.

MockExpressRequest
const MockExpressRequest = require('mock-express-request');
import MockExpressRequest from 'mock-express-request';
This package is CommonJS-only. Attempting to use ES module import syntax will result in a TypeError.
MockExpressRequest (instance)
const request = new MockExpressRequest({ method: 'GET', url: '/test' });
The primary use case is to instantiate the class with an options object to configure the mock request properties.

Demonstrates how to create basic and advanced mock Express HTTP request objects, configuring method, URL, headers, cookies, and body, then accessing these properties.

const MockExpressRequest = require('mock-express-request'); // Basic usage: Create a mock GET request to a specific URL const basicRequest = new MockExpressRequest({ method: 'GET', url: '/api/users/123?profile=full' }); console.log(`Method: ${basicRequest.method}`); // Expected: GET console.log(`URL: ${basicRequest.url}`); // Expected: /api/users/123?profile=full console.log(`Path: ${basicRequest.path}`); // Expected: /api/users/123 console.log(`Query 'profile': ${basicRequest.param('profile')}`); // Expected: full // More advanced usage: Mock a PUT request with headers and cookies const advancedRequest = new MockExpressRequest({ method: 'PUT', url: '/data/item/abc', cookies: { session_id: 'MY_SESSION_TOKEN', remember_me: 'true' }, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer ABCDEFG' }, body: { name: 'New Item', value: 42 } }); console.log(`Host: ${advancedRequest.hostname}`); // Access Express-like properties (often defaults to 'localhost') console.log(`Request body: ${JSON.stringify(advancedRequest.body)}`); console.log(`Cookie 'session_id': ${advancedRequest.cookies.session_id}`); console.log(`Header 'Content-Type': ${advancedRequest.get('Content-Type')}`); // Using .get() for headers
Debug
Known issues
breakingThis package is effectively unmaintained since 2017. It may not be compatible with newer versions of Node.js or Express.js, potentially leading to unexpected behavior or missing features found in modern Express request objects.
fix
Consider migrating to actively maintained alternatives like `node-mocks-http` or `@jest-mock/express` for modern Express environments.
affects: All versions
gotchaThe package exclusively uses CommonJS `require()`. It does not provide ES Module exports, and attempting to use `import` syntax will result in a runtime error.
fix
Always use `const MockExpressRequest = require('mock-express-request');` for importing the library.
affects: All versions
gotchaThe mock objects created by this library are simple POJOs extended with Express-like methods. They might not fully replicate the complex behavior, event emitters, or stream aspects of a real Express request, which can be crucial for testing advanced middleware.
fix
For comprehensive testing of complex Express features, consider using integration tests with tools like Supertest, or more robust mocking libraries that offer deeper Express API simulation and Jest/Sinon integration.
affects: All versions
Errors
Common errors & fixes
TypeError: MockExpressRequest is not a constructor
Attempting to import the CommonJS module using ES Module `import` syntax (e.g., `import MockExpressRequest from 'mock-express-request';`).
fix
Change the import statement to CommonJS `const MockExpressRequest = require('mock-express-request');`.
TypeError: request.body is undefined
While `body` can be passed to the constructor, Express typically populates `req.body` via body-parser middleware. This mock might not automatically process raw request data into `req.body` in the same way, leading to `undefined` if not explicitly set in the mock constructor options.
fix
Ensure `req.body` is explicitly defined in the `MockExpressRequest` constructor options, for example: `new MockExpressRequest({ body: { key: 'value' } })`.
Upgrade
Version history
0.2.2latest on npm
Audit
Dependencies
mock-reqrequiredCore dependency for request mocking functionality, as stated in the README.
Agent activity
2 hits · last 30 days
node
2
Resources
mock-express-request — npm install mock-express-request · libregistry