parrot-middleware is an Express.js middleware designed for mocking HTTP requests in development and testing environments. It allows developers to define a collection of 'scenarios', each comprising a list of request-response pairs. Based on the active scenario, the middleware intercepts matching incoming requests and serves the predefined mock responses. It provides dedicated control routes (`/parrot/scenario` for setting/getting the active scenario, and `/parrot/scenarios` for inspecting all available scenarios) to manage its behavior dynamically. Currently at stable version 5.3.0, the package sees active maintenance with frequent patch and minor releases, ensuring compatibility and addressing issues. Its key differentiator is the scenario-driven approach, making it easy to simulate complex backend states or error conditions without modifying frontend code or spinning up a full backend.
npm install parrot-middlewareVerified import paths — ran on the pinned version, not inferred.
This example sets up an Express server with `parrot-middleware`, defining two basic scenarios. It demonstrates how to initialize the middleware with scenario definitions and provides instructions on how to activate a scenario via a POST request and then fetch the mocked data.
Consult the main Parrot repository's changelog or migration guides for core breaking changes. Thoroughly test existing mock configurations after upgrading.
Ensure `app.use(parrot(scenarios));` is placed early in your Express application's middleware chain, typically before most `app.get()`, `app.post()` etc., route definitions.
Add `app.use(express.json());` (or `app.use(bodyParser.json());` for older Express versions) before `app.use(parrot(scenarios));` in your server setup.
For POST, PUT, DELETE, etc., mocks, explicitly add `{ request: { path: '/your_path', method: 'POST' }, response: ... }` to your scenario definitions.Review your middleware chain and scenario response logic. Ensure that once a mock response is served, no subsequent middleware attempts to send another response. Also, verify `next()` is called correctly if `parrot-middleware` doesn't handle a request.
If using Node.js modules (ESM), use `import parrot from 'parrot-middleware';`. If strictly CommonJS, ensure your `package.json` does not have `"type": "module"` and use `const parrot = require('parrot-middleware');`.Double-check the scenario name for typos, case sensitivity, and ensure it corresponds precisely to a key in your `scenarios.js` (or equivalent) definition.