@equinor/fusion-openapi-mock-server

中文文档
1.0.1发布于 2026/11/12ISCTypeScriptES Module近 30 天下载 2191

Standalone HTTP server that serves @equinor/fusion-openapi-mock fakes from a directory of OpenAPI specs, addressable directly by tests (e.g. Playwright) as well as local dev

原始使用文档
文档目录6 节
  1. When to use this package
  2. Installation
  3. Quick start
  4. Entry points
  5. Migration: path-to-regexp v9
  6. Docs

@equinor/fusion-openapi-mock-server

A standalone HTTP server for @equinor/fusion-openapi-mock: it serves every service it discovers from a directory of OpenAPI specs at its own address, so a test runner (e.g. Playwright) can point service discovery at one origin and drive per-test overrides directly over HTTP — no Vite or dev-server process involved.

When to use this package

  • You want a mock backend you can start and stop independently of your app's build tool (Vite, a bundled/staged build, or plain Playwright).
  • You want Playwright tests to override a specific service's response for one test (e.g. "context resolution fails"), then reset back to the generated baseline afterward.
  • You already have @equinor/fusion-openapi-mock specs and want them served over HTTP instead of wired into dev-server middleware.

Installation

pnpm add -D @equinor/fusion-openapi-mock-server

Quick start

import { createMockServer } from '@equinor/fusion-openapi-mock-server';

const server = createMockServer().use('fusion').use('./mocks');
const { url } = await server.start({ port: 4010 });
// url -> 'http://localhost:4010'

await server.close();

Or from the CLI:

pnpm exec fusion-mock --preset=fusion ./mocks --port 4010

Sources are layered from left to right, so ./mocks replaces same-key services from the bundled fusion preset. With no source argument, the CLI reads ./mocks; with no --port, the operating system assigns a free port.

Entry points

ImportPurpose
@equinor/fusion-openapi-mock-servercreateMockServer, createMockAuth, createMockAnalytics (read, wait for, and reset recorded analytics in tests), and their server/session types.
@equinor/fusion-openapi-mock-server/discoveryDefine executable <name>.mock.ts modules with defineService, or use lower-level discoverServices, createService, and createRouter APIs.
@equinor/fusion-openapi-mock-server/presetsRegistry of bundled preset loaders.
@equinor/fusion-openapi-mock-server/presets/fusionfusionPreset, defineRolesV2Mock, Fusion service definitions, defineAnalyticsMock (a mock Monitor service that receives, records, and seeds analytics), defineAppFeatureEventsMock (the Apps service's POST /apps/feature-events/query over those events), and parseOtlpLogsRequest / parseOtlpResourceLog / projectAnalyticsEvent for reading analytics the way Fusion's analytics pipeline does.

The mock server exposes generated service responses, source-defined field/path overrides, programmable middleware, and runtime operation overrides. Start with directory discovery; use the lower-level discovery entry point only when a faker function or middleware route cannot be expressed in JSON or YAML.

Migration: path-to-regexp v9

This major release changes captured parameters for middleware routes registered through ServiceBuilder.middleware or the exported createRouter. Review handlers that read RouteContext.params, especially routes with multiple parameters in one segment.

router.get('/:file.:ext', (_req, res, { params }) => res.json(params));
// Request: /foo.json.html (relative to the service)
// Before (v8): { file: 'foo.json', ext: 'html' }
// Now (v9):    { file: 'foo', ext: 'json.html' }

Prefer separate segments for independently meaningful values, updating the mock route and test clients together:

router.get('/files/:file/extensions/:ext', (_req, res, { params }) => res.json(params));
// Request: /files/foo.json/extensions/html
// params = { file: 'foo.json', ext: 'html' }

If existing URLs must stay unchanged, capture the entire segment and explicitly split it at the last dot in your handler rather than relying on ambiguous matcher captures:

router.get('/:filename', (_req, res, { params }) => {
  const filename = String(params.filename);
  const separator = filename.lastIndexOf('.');
  if (separator <= 0 || separator === filename.length - 1) {
    res.statusCode = 400;
    res.json({ error: 'Expected a filename with an extension' });
    return;
  }
  res.json({ file: filename.slice(0, separator), ext: filename.slice(separator + 1) });
});
// Request: /foo.json.html -> { file: 'foo.json', ext: 'html' }

Direct createRouter usage still sets trailing: false. A route /files/ matches /files/, not /files or /files//; optional trailing slashes have not been enabled. The standalone server already normalizes empty path segments before service middleware matching, so register /files there: service requests /files, /files/ and /files// reach that same route. This normalization is unchanged by the upgrade. Simple routes such as /users/:id are unaffected by the capture change.

Add regressions that assert both the HTTP response and params for dotted values, invalid filenames and trailing-slash variants. The dependency is ESM-only and requires Node 22 or newer; keep using this package's ESM entry points.

Docs

  • Getting started — directory convention, the bundled fusion preset, override mechanisms, layering, and running the server via the CLI or programmatically
  • Testing with Playwright — per-session user selection, switching, scope-aware token issuance, operation overrides, reset, and the full route reference
  • Test help articles locally — serve local help docs as a mock help service with defineHelpArticlesMock, so the dev portal opens the articles an app requests with useHelpCenter()
  • Test analytics locally — receive, record, and seed the analytics an app tracks with useTrackFeature(), check them in Playwright with createMockAnalytics, and answer the app-feature events query with defineAnalyticsMock and defineAppFeatureEventsMock

版本与发布记录

@equinor/fusion-openapi-mock-server 共发布 16 个版本,首次发布于 2026/08/26;过去 12 个月发布了 16 个版本。

版本发布日期距今
1.0.1latest2026/11/121 天前
1.0.1-next.0next2026/11/112 天前
1.0.02026/11/112 天前
0.5.4-next.02026/10/2816 天前
0.5.32026/10/1727 天前
0.5.3-trusted-publishing-20261015-9dfe7602cd2a49a8a9df00d62c26e7304a82a85atrusted-publishing-202610152026/10/1629 天前

关于 @equinor/fusion-openapi-mock-server 的常见问题

@equinor/fusion-openapi-mock-server 是什么?

Standalone HTTP server that serves @equinor/fusion-openapi-mock fakes from a directory of OpenAPI specs, addressable directly by tests (e.g. Playwright) as well as local dev

如何安装 @equinor/fusion-openapi-mock-server?

在项目目录运行 npm install @equinor/fusion-openapi-mock-server,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 fusion-mock,可以用 npx fusion-mock 直接运行。

@equinor/fusion-openapi-mock-server 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@equinor/fusion-openapi-mock-server 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@equinor/fusion-openapi-mock-server 可以商用吗?

@equinor/fusion-openapi-mock-server 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@equinor/fusion-openapi-mock-server 的下载量有多少?

根据 npm 官方下载统计,@equinor/fusion-openapi-mock-server 在 2026/10/14 – 2026/11/12 这 30 天内被下载 2,191 次,日均约 73 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@equinor/fusion-openapi-mock-server 还在维护吗?

最新版本 1.0.1 发布于 2026/11/12(1 天前);至今共发布 16 个版本,其中过去 12 个月发布了 16 个;首次发布于 2026/08/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。