Scenario Mock Server
Scenario Mock Server is a Node.js library for creating highly configurable mock servers driven by user-defined scenarios. Currently stable at version 1.2.0, it is actively maintained with regular minor and patch releases, recently adding features like scenario grouping in the UI and exposing request headers to response functions. Key differentiators include an integrated web UI for dynamic scenario selection, a 'cookie mode' for multi-user isolation allowing each client to manage their own scenario state, and support for parallel testing through custom `sms-scenario-id` and `sms-context-id` headers, which bypass server-side scenario selection for specific requests. It can run as a standalone server or integrate into an existing Express application.
Common errors
-
Error: listen EADDRINUSE: address already in use :::3000
cause The default port (3000) for the mock server is already occupied by another process.fixSpecify a different port in the `options` object when calling `run()` or `createExpressApp()`, e.g., `{ options: { port: 3001 } }`. -
ReferenceError: require is not defined
cause Attempting to use `require()` in an ECMAScript Module (ESM) context without proper configuration (e.g., `"type": "module"` in `package.json` but using `require`).fixSwitch to `import { run } from 'scenario-mock-server';` syntax, or ensure your environment is configured for CommonJS modules, or use a build tool to transpile. -
Scenario 'my-missing-scenario-id' not found or active for path '/api/data'
cause The requested scenario ID does not exist in your defined scenarios, or the server is not configured to select it.fixVerify the scenario ID spelling. Ensure the scenario is correctly defined in the `scenarios` object. If using the UI, select the correct scenario. If using `sms-scenario-id` header, confirm the header value matches an existing scenario group key.
Warnings
- breaking The package was renamed from `data-mocks-server` to `scenario-mock-server`. Projects upgrading or migrating from the old package name must update their `package.json` and all import/require statements.
- gotcha Custom headers `sms-scenario-id` and `sms-context-id` for parallel testing are explicitly NOT supported when `cookieMode` is enabled. If you need per-request scenario selection, ensure `cookieMode` is set to `false` (default) or not configured.
- breaking Version 1.0.0 marked the first major release of the `scenario-mock-server` under its new name. While specific breaking changes from any pre-1.0.0 versions of `data-mocks-server` are not detailed in the release notes, users migrating from unversioned or pre-1.0.0 iterations of the original project should expect potential API shifts.
Install
-
npm install scenario-mock-server -
yarn add scenario-mock-server -
pnpm add scenario-mock-server
Imports
- run
const { run } = require('scenario-mock-server');import { run } from 'scenario-mock-server'; - createExpressApp
const { createExpressApp } = require('scenario-mock-server').createExpressApp;import { createExpressApp } from 'scenario-mock-server'; - HttpMock
import { HttpMock } from 'scenario-mock-server';import type { HttpMock } from 'scenario-mock-server';
Quickstart
import { run } from 'scenario-mock-server';
run({
scenarios: {
item: [
{
path: '/api/test-me',
method: 'GET',
response: { data: { blue: 'yoyo' } },
},
],
cheese: [
{
path: '/api/test-me',
method: 'GET',
response: { data: { blue: 'cheese' } },
},
],
},
options: {
port: process.env.MOCK_SERVER_PORT ? parseInt(process.env.MOCK_SERVER_PORT) : 3000,
cookieMode: false
}
});
console.log('Scenario Mock Server running. Visit http://localhost:3000 to manage scenarios.');