@chat-adapter/tests

中文文档
4.44.0发布于 2026/11/02MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 17.4万

Vitest factories, matchers, and setup utilities for testing Chat SDK adapters and bots

原始使用文档
文档目录14 节
  1. Installation
  2. Factories
  3. createMockAdapter(name?, overrides?)
  4. createMockState()
  5. createMockChatInstance(options?)
  6. createTestMessage(id, text, overrides?)
  7. mockLogger / createMockLogger()
  8. Matchers
  9. Auto-register via setup file
  10. Manual registration
  11. Available matchers
  12. Audience
  13. AI Coding Agents
  14. License

@chat-adapter/tests

npm package: @chat-adapter/tests

Agent Stack MIT License

Vitest factories, matchers, and setup utilities for testing Chat SDK adapters and bots.

Installation

pnpm add -D @chat-adapter/tests

This package has chat and vitest as peer dependencies — they should already be in your project.

Factories

import {
  createMockAdapter,
  createMockChatInstance,
  createMockState,
  createTestMessage,
  mockLogger,
} from "@chat-adapter/tests";

createMockAdapter(name?, overrides?)

Returns an Adapter with every method as vi.fn() and sensible defaults. Pass overrides to swap individual methods:

const adapter = createMockAdapter("slack", {
  postMessage: vi.fn().mockResolvedValue({ id: "msg-7", raw: {} }),
});

createMockState()

Returns a StateAdapter backed by in-memory Maps — subscriptions, locks, KV, lists, and queues all work end-to-end. Includes a cache: Map<string, unknown> for direct inspection.

createMockChatInstance(options?)

Returns a ChatInstance with every process* handler as vi.fn(). Useful for adapter authors verifying their adapter dispatches incoming events through the right hook.

const state = createMockState();
const chat = createMockChatInstance({ state });
await myAdapter.handleWebhook(req); // your adapter under test
expect(chat.processMessage).toHaveBeenCalledOnce();

createTestMessage(id, text, overrides?)

Builds a Message with parsed markdown AST already wired up.

mockLogger / createMockLogger()

mockLogger is a shared Logger for tests that don't care about isolation. createMockLogger() returns a fresh one per call.

Matchers

Vitest custom matchers covering the most common Chat SDK assertions.

Auto-register via setup file

// vitest.config.ts
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    setupFiles: ["@chat-adapter/tests/setup"],
  },
});

Manual registration

import { matchers } from "@chat-adapter/tests/matchers";
expect.extend(matchers);

Available matchers

MatcherAsserts
expect(adapter).toHavePosted(threadId, textPattern?)adapter.postMessage was called for this thread (and message text matches textPattern if given)
expect(adapter).toHaveEdited(threadId, messageId, textPattern?)adapter.editMessage was called for this message (and text matches textPattern if given)
expect(adapter).toHaveDeleted(threadId, messageId)adapter.deleteMessage was called for this message
expect(adapter).toHaveReactedWith(threadId, messageId, emoji)adapter.addReaction was called with the emoji (string or EmojiValue.name)
expect(adapter).toHaveStartedTyping(threadId)adapter.startTyping was called for this thread
expect(adapter).toHavePostedToChannel(channelId, textPattern?)adapter.postChannelMessage was called for this channel
expect(chat).toHaveDispatched(handler)The named process* handler on the mock ChatInstance was called
expect(state).toBeSubscribedTo(threadId)state.isSubscribed(threadId) resolves to true (async — needs await)
expect(adapter).toHavePosted("slack:C1:t1", /hello/);
expect(adapter).toHaveEdited("slack:C1:t1", "msg-1", /updated/);
expect(adapter).toHaveDeleted("slack:C1:t1", "msg-1");
expect(adapter).toHaveReactedWith("slack:C1:t1", "msg-1", "thumbsup");
expect(adapter).toHaveStartedTyping("slack:C1:t1");
expect(adapter).toHavePostedToChannel("slack:C1");
expect(chat).toHaveDispatched("processMessage");
await expect(state).toBeSubscribedTo("slack:C1:t1");

Text-pattern matchers (toHavePosted, toHaveEdited, toHavePostedToChannel) extract a comparable string from AdapterPostableMessage — handling plain strings, PostableMarkdown.markdown, PostableRaw.raw, and PostableCard.fallbackText. AST-shaped messages (PostableAst) and cards without fallbackText aren't text-matchable; assert without textPattern and inspect mock.calls directly for deeper checks.

Audience

  • Bot authors — drive simulated events through your handlers, assert on outbound calls.
  • Adapter authors — verify your Adapter implementation routes webhooks through the right ChatInstance.process* hook with the right normalized payload.

Adapter-specific helpers (e.g. signed Slack webhook builders, Teams claim builders) live in each adapter's own /testing subpath, not in this kit.

AI Coding Agents

If you use an AI coding agent such as OpenAI Codex, Claude Code, or Cursor, install the Chat SDK skill so it knows the SDK APIs, adapter patterns, and project conventions before writing code.

npx skills add vercel/chat

The skill references bundled documentation in node_modules/chat/docs, plus adapter guides and starter templates in the published package.

You can also install the Vercel Plugin for a broader agent toolkit — it includes the Chat SDK skill alongside specialist agents, agent slash commands, and more:

npx plugins add vercel/vercel-plugin

The plugin is optional; the skill alone is enough to build with Chat SDK.

For agent-readable documentation, see chat-sdk.dev/llms.txt (page index) or chat-sdk.dev/llms-full.txt (full text).

License

MIT

版本与发布记录

@chat-adapter/tests 共发布 19 个版本,首次发布于 2026/05/18;过去 12 个月发布了 19 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.44.0latest2026/11/026 天前
4.43.02026/10/1921 天前
4.42.02026/10/1425 天前
4.41.12026/09/291 个月前
4.41.02026/09/181 个月前
4.40.02026/09/052 个月前

关于 @chat-adapter/tests 的常见问题

@chat-adapter/tests 是什么?

Vitest factories, matchers, and setup utilities for testing Chat SDK adapters and bots

如何安装 @chat-adapter/tests?

在项目目录运行 npm install @chat-adapter/tests,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:chat(^4.28.1)、vitest(^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@chat-adapter/tests 支持 TypeScript 吗?

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

@chat-adapter/tests 支持 ESM 还是 CommonJS?

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

@chat-adapter/tests 可以商用吗?

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

@chat-adapter/tests 的下载量有多少?

根据 npm 官方下载统计,@chat-adapter/tests 在 2026/10/10 – 2026/11/08 这 30 天内被下载 174,404 次,日均约 5,813 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chat-adapter/tests 还在维护吗?

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