mock-http-server is a Node.js library designed to create controllable HTTP and HTTPS server mocks, primarily for use in functional and integration tests. It allows developers to define expected incoming requests by method and path, and then configure custom responses including status codes, headers, and body content. The current stable version is 1.4.5, with recent minor releases adding features like request history clearing, improved body parsing for various content types (text/plain, urlencoded), and specific port retrieval methods. Its release cadence appears to be feature-driven, with new functionalities and minor fixes arriving periodically. Key differentiators include its explicit control over request matching and response generation, the ability to inspect received requests for assertions, and support for both HTTP and HTTPS protocols. It aims to provide a reliable and isolated testing environment without actual network calls to external services, allowing for consistent and fast test execution.
npm install mock-http-serverVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up, start, and stop a mock HTTP server, define a request handler, make a request against it, and assert on the response and recorded requests within a test suite (e.g., Jest or Mocha).
Review any `server.requests()` calls that use multiple filtering conditions. If 'OR' logic is still desired, implement custom filtering using the `filter` option or perform multiple `requests()` calls and combine the results manually.
Always ensure `server.stop()` is called in an `afterAll` or `afterEach` block. For isolated tests, use `server.resetHandlers()` and `server.clearRequests()` in `afterEach` to clean up server state.
For ES Modules, use `import ServerMock from 'mock-http-server';`. For CommonJS, use `const ServerMock = require('mock-http-server');`.Create a `declarations.d.ts` file in your project to define types for `mock-http-server`, or consider using a different mocking library if strong TypeScript support is a critical requirement. Alternatively, suppress errors with `// @ts-ignore` at the cost of type safety.
Ensure `server.stop()` is reliably called in your test teardown (e.g., `afterEach` or `afterAll`). If the issue persists, choose a different port, or configure the server to use port `0` which lets the OS assign an available port, then retrieve it with `server.getHttpPort()` or `server.getHttpsPort()`.
Change the import statement to a default import: `import ServerMock from 'mock-http-server';`.
Verify that `server.start()` has completed successfully before making requests. Ensure the client is configured to connect to the correct host and port, especially if you're using dynamic port assignment (e.g., port `0`). Make sure there isn't a firewall blocking the connection.
Refactor your import statements to use ESM `import ServerMock from 'mock-http-server';`. If you need to mix CommonJS `require` with ESM, you can use `import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);` but this is generally discouraged for package imports.No dependency data recorded yet.