{"id":16443,"library":"mock-express-request","title":"Mock Express Request","description":"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.","status":"abandoned","version":"0.2.2","language":"javascript","source_language":"en","source_url":"https://github.com/lykmapipo/mock-express-request","tags":["javascript","express","connect","mock","stab","unit","test","spec","specification"],"install":[{"cmd":"npm install mock-express-request","lang":"bash","label":"npm"},{"cmd":"yarn add mock-express-request","lang":"bash","label":"yarn"},{"cmd":"pnpm add mock-express-request","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency for request mocking functionality, as stated in the README.","package":"mock-req","optional":false}],"imports":[{"note":"This package is CommonJS-only. Attempting to use ES module import syntax will result in a TypeError.","wrong":"import MockExpressRequest from 'mock-express-request';","symbol":"MockExpressRequest","correct":"const MockExpressRequest = require('mock-express-request');"},{"note":"The primary use case is to instantiate the class with an options object to configure the mock request properties.","symbol":"MockExpressRequest (instance)","correct":"const request = new MockExpressRequest({ method: 'GET', url: '/test' });"}],"quickstart":{"code":"const MockExpressRequest = require('mock-express-request');\n\n// Basic usage: Create a mock GET request to a specific URL\nconst basicRequest = new MockExpressRequest({\n    method: 'GET',\n    url: '/api/users/123?profile=full'\n});\n\nconsole.log(`Method: ${basicRequest.method}`); // Expected: GET\nconsole.log(`URL: ${basicRequest.url}`);       // Expected: /api/users/123?profile=full\nconsole.log(`Path: ${basicRequest.path}`);     // Expected: /api/users/123\nconsole.log(`Query 'profile': ${basicRequest.param('profile')}`); // Expected: full\n\n// More advanced usage: Mock a PUT request with headers and cookies\nconst advancedRequest = new MockExpressRequest({\n    method: 'PUT',\n    url: '/data/item/abc',\n    cookies: { session_id: 'MY_SESSION_TOKEN', remember_me: 'true' },\n    headers: {\n        'Accept': 'application/json',\n        'Content-Type': 'application/json',\n        'Authorization': 'Bearer ABCDEFG'\n    },\n    body: { name: 'New Item', value: 42 }\n});\n\nconsole.log(`Host: ${advancedRequest.hostname}`); // Access Express-like properties (often defaults to 'localhost')\nconsole.log(`Request body: ${JSON.stringify(advancedRequest.body)}`);\nconsole.log(`Cookie 'session_id': ${advancedRequest.cookies.session_id}`);\nconsole.log(`Header 'Content-Type': ${advancedRequest.get('Content-Type')}`); // Using .get() for headers","lang":"javascript","description":"Demonstrates how to create basic and advanced mock Express HTTP request objects, configuring method, URL, headers, cookies, and body, then accessing these properties."},"warnings":[{"fix":"Consider migrating to actively maintained alternatives like `node-mocks-http` or `@jest-mock/express` for modern Express environments.","message":"This 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.","severity":"breaking","affected_versions":"All versions"},{"fix":"Always use `const MockExpressRequest = require('mock-express-request');` for importing the library.","message":"The package exclusively uses CommonJS `require()`. It does not provide ES Module exports, and attempting to use `import` syntax will result in a runtime error.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Change the import statement to CommonJS `const MockExpressRequest = require('mock-express-request');`.","cause":"Attempting to import the CommonJS module using ES Module `import` syntax (e.g., `import MockExpressRequest from 'mock-express-request';`).","error":"TypeError: MockExpressRequest is not a constructor"},{"fix":"Ensure `req.body` is explicitly defined in the `MockExpressRequest` constructor options, for example: `new MockExpressRequest({ body: { key: 'value' } })`.","cause":"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.","error":"TypeError: request.body is undefined"}],"ecosystem":"npm"}