{"id":27867,"library":"http-test-server","title":"http-test-server","description":"A simple, promise-based HTTP server for integration testing in Node.js. v3.0.0 provides an async factory function that starts a server on a random port and returns an object with `baseUrl` and `shutdown()` method. It supports automatic shutdown and collects request body as Buffer. Unlike alternatives like `nock` or `supertest`, this uses a real HTTP server, making it suitable for end-to-end API testing. Released with TypeScript definitions. Maintenance mode since 2022.","status":"maintenance","version":"3.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/kesla/http-test-server","tags":["javascript","typescript"],"install":[{"cmd":"npm install http-test-server","lang":"bash","label":"npm"},{"cmd":"yarn add http-test-server","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-test-server","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Ensures graceful shutdown of the HTTP server, preventing hanging sockets in tests.","package":"http-shutdown"},{"reason":"Converts the incoming request stream to a promise that resolves to the full body Buffer.","package":"stream-to-promise"}],"imports":[{"note":"The module exports a default async function. Named import will not work.","wrong":"import { httpTestServer } from 'http-test-server'","symbol":"default","correct":"import httpTestServer from 'http-test-server'"},{"note":"In CommonJS, require returns the default export directly.","wrong":"const { httpTestServer } = require('http-test-server')","symbol":"default","correct":"const httpTestServer = require('http-test-server')"},{"note":"TypeScript users can import `ShutdownOptions` type from the package's types.","wrong":"","symbol":"ShutdownOptions","correct":"import type { ShutdownOptions } from 'http-test-server'"}],"quickstart":{"code":"import httpTestServer from 'http-test-server';\nimport got from 'got';\n\nconst server = await httpTestServer((req, res) => {\n  res.end(req.url);\n});\n\nconst { body } = await got(`${server.baseUrl}/hello`);\nconsole.log(body); // '/hello'\n\nawait server.shutdown();","lang":"typescript","description":"Creates a server that echoes the request URL, sends a GET request, and shuts down."},"warnings":[{"fix":"Use `await httpTestServer(handler)` to obtain the server object.","message":"The server is started asynchronously; you must await the function call. Not doing so leads to undefined behavior.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Access `req.body` inside the request handler; it will be a Buffer if the client sends a body.","message":"The request body is collected as a Buffer via stream-to-promise. It is only available after the handler is called; do not access `req.body` before reading the stream yourself.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Use URL constructor: `new URL('/foo', server.baseUrl).href` to avoid double slashes.","message":"The `baseUrl` property includes protocol and port but no trailing slash. Concatenating paths directly may lead to double slashes if path starts with '/'.","severity":"deprecated","affected_versions":">=3.0.0"},{"fix":"No action needed; the OS assignes a free port automatically.","message":"The server listens on a random port (port 0). That port must be free on the system, but conflicts are unlikely.","severity":"gotcha","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Use `import httpTestServer from 'http-test-server'` (default import).","cause":"Incorrect import; using named import instead of default.","error":"TypeError: httpTestServer is not a function"},{"fix":"Add `await` before httpTestServer: `const server = await httpTestServer(...)`.","cause":"Not awaiting the httpTestServer call; server object is a promise.","error":"Cannot read properties of undefined (reading 'baseUrl')"},{"fix":"Ensure the server promise resolves before using server.baseUrl.","cause":"Making requests before server is fully started (race condition).","error":"Error: read ECONNRESET"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}