{"id":15344,"library":"jest-mock-server","title":"Jest Mock Server","description":"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.","status":"active","version":"0.1.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/DanielHreben/jest-mock-server","tags":["javascript","typescript"],"install":[{"cmd":"npm install jest-mock-server","lang":"bash","label":"npm"},{"cmd":"yarn add jest-mock-server","lang":"bash","label":"yarn"},{"cmd":"pnpm add jest-mock-server","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for integrating with Jest's test runner and assertion API as a peer dependency.","package":"jest","optional":false}],"imports":[{"note":"The `MockServer` class is a named export, not a default export. Attempting to import it as a default will result in `undefined` or a runtime error.","wrong":"import MockServer from 'jest-mock-server';","symbol":"MockServer","correct":"import { MockServer } from 'jest-mock-server';"},{"note":"While primarily designed for ESM, CommonJS environments can import `MockServer` as a named property from the module's exports.","wrong":"const MockServer = require('jest-mock-server'); // Incorrectly assumes a default export for the entire module","symbol":"MockServer (CommonJS)","correct":"const { MockServer } = require('jest-mock-server');"},{"note":"While `jest-mock-server` uses Koa's API, Koa types like `Context` are typically imported directly from 'koa' if needed for explicit type annotations outside of the handler implementation.","symbol":"Route definitions (type)","correct":"import type { Context } from 'koa';"}],"quickstart":{"code":"import { MockServer } from 'jest-mock-server';\nimport fetch from 'node-fetch'; // Or any other http client like axios\n\ndescribe('Testing HTTP client with jest-mock-server', () => {\n  const server = new MockServer();\n\n  // Start and stop the mock server for each test suite\n  beforeAll(() => server.start());\n  afterAll(() => server.stop());\n  // Reset routes before each test to ensure isolation\n  beforeEach(() => server.reset());\n\n  it('handles multiple requests with sequential responses', async () => {\n    // Define a GET route for the root path\n    const route = server\n      .get('/')\n      // Mock the first response to return status 200\n      .mockImplementationOnce((ctx) => {\n        ctx.status = 200;\n        ctx.body = 'First response';\n      })\n      // Mock the second response to return status 201\n      .mockImplementationOnce((ctx) => {\n        ctx.status = 201;\n        ctx.body = 'Second response';\n      });\n\n    // Get the URL where the server is listening (random free port by default)\n    const url = server.getURL();\n\n    // Make the first request\n    const res1 = await fetch(url);\n    expect(res1.status).toBe(200);\n    expect(await res1.text()).toBe('First response');\n\n    // Make the second request\n    const res2 = await fetch(url);\n    expect(res2.status).toBe(201);\n    expect(await res2.text()).toBe('Second response');\n\n    // Make a third request - since only two mocks were defined, it will return 404 by default\n    const res3 = await fetch(url);\n    expect(res3.status).toBe(404);\n\n    // Use Jest's assertion API to verify the route was called three times\n    expect(route).toHaveBeenCalledTimes(3);\n  });\n});","lang":"typescript","description":"Demonstrates setting up a mock server, defining a dynamic route with Koa context, and asserting calls using Jest API with an HTTP client."},"warnings":[{"fix":"Ensure your project's runtime environment is using Node.js v14 or higher. Update your Node.js version if necessary.","message":"The package explicitly switched its minimum supported Node.js version to v14 in `v0.0.4`. While newer Node.js versions are likely compatible, projects running on Node.js versions older than 14 are not supported.","severity":"gotcha","affected_versions":"<=0.0.3"},{"fix":"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.","message":"`jest-mock-server` declares `jest` as a peer dependency. If Jest is not installed in your project, or if there's a significant version mismatch, it may lead to runtime errors or unexpected behavior.","severity":"gotcha","affected_versions":"*"},{"fix":"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.","message":"As `jest-mock-server` is currently in a `0.x.x` version series, it does not strictly adhere to semantic versioning. This means that breaking changes may be introduced in minor or patch releases without a major version bump, potentially causing unexpected issues upon upgrade.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"For ESM, use `import { MockServer } from 'jest-mock-server';`. For CommonJS, use `const { MockServer } = require('jest-mock-server');`.","cause":"Attempting to use `MockServer` as a default import when it is a named export, or incorrect CommonJS `require` usage.","error":"TypeError: jest_mock_server_1.MockServer is not a constructor (or similar 'is not a function' error)"},{"fix":"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.","cause":"The `jest` package, a required peer dependency, is either not installed or its version conflicts with the requirements of `jest-mock-server`.","error":"npm ERR! ERESOLVE unable to resolve dependency tree (or similar peer dependency warning/error)"}],"ecosystem":"npm"}