Mockttp is a versatile JavaScript library for intercepting, transforming, and testing HTTP requests and responses. It serves as a powerful tool for integration testing by allowing developers to stub server responses, verify real HTTP requests, and even act as a transparent proxy for complex network debugging. Unlike in-process stubbing solutions, Mockttp operates at the network level, ensuring accurate testing of how entire stacks handle real-world HTTP traffic. The current stable version is v4.3.1, with major versions typically released on an annual cadence, focusing on modern Node.js environments and developer experience. Key differentiators include its ability to run tests uniformly in both Node.js and browser environments, support for transparent proxying and HTTPS interception (with built-in CA certificate generation), strong TypeScript typing, and features for safe, parallel test execution with enhanced debuggability.
npm install mockttpVerified import paths — ran on the pinned version, not inferred.
Demonstrates setting up a local Mockttp server with HTTPS, defining a POST rule with a delay and a WebSocket passthrough rule with request transformation, and starting the server.
Upgrade your Node.js environment to version 20.0.0 or newer. Consider using nvm or similar tools for managing Node.js versions.
Ensure your Node.js environment is at least v14.14.0. Upgrade if you are on an unsupported version.
Replace old methods with their `.forX()` equivalents, such as `mockServer.forGet()`, `mockServer.forPost()`, or `mockServer.forAnyRequest()`.
Switch to the corresponding asynchronous `body.getX()` methods (e.g., `body.getDecodedBuffer()`, `body.getText()`, `body.getJson()`, `body.getFormData()`) for all content decoding.
Pass the generated `https` configuration to the mock server, and configure your HTTP client or operating system to trust the CA certificate provided by `mockttp.generateCACertificate()` or `server.port` / `server.url`.
Ensure you are using the correct import style for your module system: `import { getLocal } from 'mockttp';` for ESM, or `const mockttp = require('mockttp'); const server = mockttp.getLocal();` for CJS.Update your code to use the modern, explicit rule definition methods: `server.forGet()`, `server.forPost()`, `server.forAnyRequest()`, etc.
Upgrade your Node.js installation to version 20.0.0 or newer. Use a Node Version Manager (like nvm) to easily switch versions.
Configure your HTTP client (e.g., `process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'` for Node.js, or add the generated CA certificate to your client's trust store) or operating system to explicitly trust Mockttp's CA certificate.
No dependency data recorded yet.