{"id":17714,"library":"in-process-request","title":"In-Process HTTP Request Executor","description":"in-process-request is a lightweight Node.js library designed to execute HTTP handler functions directly within the current process, bypassing the need to start and manage a local HTTP server. This makes it ideal for integration testing of API routes or for internal service-to-service communication within the same application without network overhead. The library provides a unified interface for interacting with handlers from popular web frameworks including Express.js (v3, v4, v5), Koa (v2), Hapi (v19, v20), NestJS (v7), Fastify (v3), Connect (v3), Polka, and Apollo Server (v2, v3). Its current stable version is 0.3.1, with releases typically focusing on bug fixes, dependency updates, and ensuring compatibility with newer versions of supported frameworks. It differentiates itself by offering a robust mocking layer for HTTP requests and responses, allowing for precise control over the input and examination of the output.","status":"active","version":"0.3.1","language":"javascript","source_language":"en","source_url":"https://github.com/janaz/in-process-request","tags":["javascript","express","expressjs","koa","connect","polka","hapi","nestjs","fastify","typescript"],"install":[{"cmd":"npm install in-process-request","lang":"bash","label":"npm"},{"cmd":"yarn add in-process-request","lang":"bash","label":"yarn"},{"cmd":"pnpm add in-process-request","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For modern TypeScript and ESM projects, use the default import. CommonJS `require` is also fully supported as shown in the README examples.","wrong":"const inProcessRequest = require('in-process-request');","symbol":"inProcessRequest","correct":"import inProcessRequest from 'in-process-request';"},{"note":"Hapi integration requires using the named `HapiListener` class to adapt Hapi's server listener model. It's available as a named export in ESM and as a property on the main `require` object in CJS.","wrong":"const { HapiListener } = require('in-process-request');\nconst HapiListener = require('in-process-request').HapiListener;","symbol":"HapiListener","correct":"import { HapiListener } from 'in-process-request';"},{"note":"When using TypeScript, the `InProcessRequestHandler` type can be imported for precise type hinting of the function returned by `inProcessRequest()`.","symbol":"InProcessRequestHandler","correct":"import inProcessRequest, { InProcessRequestHandler } from 'in-process-request';\nlet handler: InProcessRequestHandler;"}],"quickstart":{"code":"import inProcessRequest from 'in-process-request';\nimport express from 'express';\n\n// Create a basic Express application\nconst myApp = express();\nmyApp.use(express.json()); // Enable JSON body parsing\n\nmyApp.get('/api/test', (req, res) => {\n  res.json({ message: 'Hello from API!', query: req.query });\n});\n\nmyApp.post('/api/data', (req, res) => {\n  if (!req.body || Object.keys(req.body).length === 0) {\n    return res.status(400).json({ error: 'Request body is empty' });\n  }\n  res.status(201).json({ received: req.body, timestamp: new Date().toISOString() });\n});\n\n// Create an in-process handler for the Express app\nconst myAppHandler = inProcessRequest(myApp);\n\nasync function runTests() {\n  // Test a GET request\n  const getResponse = await myAppHandler({\n    path: '/api/test?param1=value1&param2=value2',\n    method: 'GET',\n    headers: { 'Accept': 'application/json' }\n  });\n\n  console.log('--- GET Request Result ---');\n  console.log('Status Code:', getResponse.statusCode);\n  if (getResponse.isUTF8) {\n    const body = getResponse.body.toString('utf8');\n    console.log('Body:', body);\n    console.log('Parsed JSON:', JSON.parse(body));\n  }\n\n  // Test a POST request with a JSON body\n  const postResponse = await myAppHandler({\n    path: '/api/data',\n    method: 'POST',\n    headers: { 'Content-Type': 'application/json' },\n    body: JSON.stringify({ item: 'sample', quantity: 10 })\n  });\n\n  console.log('\\n--- POST Request Result ---');\n  console.log('Status Code:', postResponse.statusCode);\n  if (postResponse.isUTF8) {\n    const body = postResponse.body.toString('utf8');\n    console.log('Body:', body);\n    console.log('Parsed JSON:', JSON.parse(body));\n  }\n}\n\nrunTests();","lang":"typescript","description":"This quickstart demonstrates how to set up an Express.js application and use `in-process-request` to execute both GET and POST requests against its handler, capturing the response details."},"warnings":[{"fix":"Check the `in-process-request` README for specific framework version compatibility. Upgrade `in-process-request` or adjust your framework version if conflicts arise.","message":"Ensure the target web framework version is explicitly supported by `in-process-request`. While the library aims for broad compatibility, changes in framework internals can lead to unexpected behavior or require updates. Refer to the package's README for the list of officially tested and supported framework versions.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Always provide a valid non-empty `path` string in the `requestOptions` object, e.g., `{ path: '/my-endpoint?query=value' }`.","message":"The `requestOptions.path` property is mandatory for every request. Omitting or providing an empty string for `path` will result in an error, as the library cannot determine which route to match.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Limit interactions with the request and response objects to standard HTTP methods, headers, body, and status codes. Avoid relying on low-level `socket` properties or stream events unless explicitly documented as supported by `in-process-request`'s mocks.","message":"The request and response objects provided to and returned from the handler are mocks, not actual `http.IncomingMessage` and `http.ServerResponse` instances. While they replicate essential properties and methods, deep reliance on specific Node.js HTTP stream or socket properties that are not explicitly mocked may lead to issues.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Ensure that `requestOptions` includes a non-empty `path` string, e.g., `handler({ path: '/api/resource', method: 'GET' })`.","cause":"The `path` property was omitted or provided as an empty string in the `requestOptions` object passed to the handler.","error":"Error: The 'path' property is mandatory in requestOptions."},{"fix":"Pass `koaApp.callback()` to `inProcessRequest`, not `koaApp`. Correct usage: `const handler = inProcessRequest(koaApp.callback());`","cause":"When integrating with Koa.js, the `inProcessRequest` function expects the raw request listener, not the Koa application instance itself.","error":"TypeError: app.callback is not a function"},{"fix":"Use the `HapiListener` helper class. Create an instance of `HapiListener`, pass it to `Hapi.server({ listener: myListener })`, and then use `myListener.handler` with `inProcessRequest`. Example: `const myListener = new HapiListener(); const server = Hapi.server({ listener: myListener }); const handler = inProcessRequest(myListener.handler);`","cause":"When integrating with Hapi, the Hapi server needs a specific custom listener provided by `in-process-request` to expose its request handler.","error":"TypeError: listener is not an instance of Server"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}