@mottyjs/test

中文文档
0.2.0发布于 2026/11/02Apache-2.0TypeScriptES Module构建来源已验证

The motty test kit: scripted models, in-memory storage and a run function for any test runner.

原始使用文档

@mottyjs/test

The test kit of the Motty SDK, used with the author's own test runner. fake and on script the models, memory is storage for a test, and run is the whole lifecycle of a request without the wire. Cases, tables and coverage stay with the runner.

bun add --dev @mottyjs/test
import { fake, memory, on, run } from "@mottyjs/test";

A request answered by a scripted model:

import { expect, test } from "bun:test";
import { model, motty } from "@mottyjs/core";
import { fake, on, run } from "@mottyjs/test";

const luna = model("openai/gpt-6-luna");

const relay = motty({ name: "relay" }).handle(({ prompt }) => luna.complete(prompt));

test("a question is answered by the small model", async () => {
  const models = fake(on(luna, "quick reply"));

  const { answer } = await run(
    relay,
    { text: "What does git stash do?" },
    { models: [models] },
  );

  expect(await answer.text()).toBe("quick reply");
});

A fake applies a connection's rules for the headers a call may state. It forwards none by default, and takes forwardHeaders as an adapter does, ahead of its scripts. It holds no headers of its own, so a name a real connection or its API states is not left out by a fake. What sent returns holds only the headers the rules let through, and the rest are warnings on the call:

const models = fake({ forwardHeaders: ["x-session"] }, on(luna, "quick reply"));

The SDK as a whole is described in the root README, and the reasons behind its design in ARCHITECTURE.md.

版本与发布记录

@mottyjs/test 共发布 4 个版本,首次发布于 2026/10/12;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.0latest2026/11/021 天前
0.1.12026/10/1321 天前
0.1.02026/10/1222 天前
0.0.0-stage2026/10/1222 天前

关于 @mottyjs/test 的常见问题

@mottyjs/test 是什么?

motty测试套件:脚本模型、内存存储和用于任何测试运行器的运行函数。

如何安装 @mottyjs/test?

在项目目录运行 npm install @mottyjs/test,或使用 pnpm add / yarn add / bun add。

@mottyjs/test 支持 TypeScript 吗?

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

@mottyjs/test 支持 ESM 还是 CommonJS?

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

@mottyjs/test 可以商用吗?

@mottyjs/test 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@mottyjs/test 还在维护吗?

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