jest-mock-server is a JavaScript/TypeScript library designed to facilitate testing of HTTP clients within a Jest testing environment. It offers a unique approach by allowing developers to define HTTP request handlers using the familiar Koa API for context manipulation and response generation, while leveraging Jest's powerful assertion API for verifying interactions with the mock server. The current stable version is 0.1.0, indicating it is still in early development, and future releases may introduce breaking changes without strict adherence to semantic versioning. Its release cadence appears to be irregular, with small incremental updates. Key differentiators include its seamless integration with both Koa for handler definition and Jest for comprehensive assertion capabilities, making it particularly suitable for projects already using these technologies.
npm install jest-mock-serverVerified import paths — ran on the pinned version, not inferred.
Demonstrates setting up a mock server, defining a dynamic route with Koa context, and asserting calls using Jest API with an HTTP client.
Ensure your project's runtime environment is using Node.js v14 or higher. Update your Node.js version if necessary.
Ensure Jest is installed as a dev dependency in your project: `npm install --save-dev jest` or `yarn add -D jest`. Consider using a compatible version of Jest as indicated by `jest-mock-server`'s peer dependency range.
Pin specific `jest-mock-server` versions in your `package.json` (e.g., `"jest-mock-server": "0.1.0"`) to prevent automatic updates to potentially breaking versions. Always review the release notes carefully before upgrading.
For ESM, use `import { MockServer } from 'jest-mock-server';`. For CommonJS, use `const { MockServer } = require('jest-mock-server');`.Install Jest as a development dependency: `npm install --save-dev jest` or `yarn add -D jest`. If a version conflict persists, try `npm install --force` or `yarn add -D jest@<compatible-version>` after checking `jest-mock-server`'s peer dependency range.