* feat(tests): add @chat-adapter/tests test kit
New package providing Vitest factories, custom matchers, and a setup file for
people building Chat SDK adapters and bots.
Factories: createMockAdapter, createMockChatInstance, createMockState (with
working in-memory subscriptions/locks/KV/queues), createTestMessage,
mockLogger / createMockLogger.
Matchers: toHavePosted(threadId, textPattern?), toHaveDispatched(handler),
toBeSubscribedTo(threadId). Auto-register via the
'@chat-adapter/tests/setup' subpath in vitest setupFiles.
chat and vitest are peer dependencies. Adapter-specific helpers (e.g. signed
Slack webhook builders) belong in each adapter's own /testing subpath, not
in this kit.
* test(integration-tests): allow @chat-adapter/tests imports in README check
* docs: add Testing page covering @chat-adapter/tests
New content/docs/testing.mdx walks bot authors and custom-adapter authors
through the kit's factories, custom matchers, and setup file. Added under
the Usage section in the sidebar, after error-handling.
Cross-link from contributing/testing.mdx clarifying that the hand-rolled
patterns there are for repo contributors building first-party adapters,
while consumers of Chat SDK should use @chat-adapter/tests.
* test(integration-tests): allow @chat-adapter/tests imports in docs check
* fix(tests): match real Adapter.postMessage signature in toHavePosted
Adapter.postMessage is (threadId: string, message: AdapterPostableMessage)
— previously the matcher read args[0] as { id: string } and args[1] as
{ text: string }, neither of which match the actual SDK shape. The matcher's
own tests fed the same wrong shape into the mock so they passed locally
while the matcher silently failed against any real bot or adapter.
Now compares args[0] as a string threadId, and extracts a comparable string
from AdapterPostableMessage's union — strings directly, PostableMarkdown
.markdown, PostableRaw.raw, and PostableCard.fallbackText. PostableAst and
fallback-less cards aren't text-matchable; documented in the JSDoc.
Tests updated to call postMessage with the real signature and to cover
each comparable AdapterPostableMessage shape.
* test(tests): add smoke tests driving matchers against a real Chat
Construct a real `Chat` with a `createMockAdapter` + `createMockState` and
exercise `Chat.thread().post()` and `.subscribe()` end-to-end. The matchers
(toHavePosted, toBeSubscribedTo) then assert against the actual call shape
the SDK uses, so a future signature drift breaks here instead of silently
agreeing with whatever wrong shape lives in the unit tests.
This is the regression guard for the postMessage-shape bug fixed in the
prior commit: each new matcher in subsequent PRs should be paired with a
smoke case here.
* feat(tests): round out adapter mutation matchers
Adds toHaveEdited, toHaveDeleted, toHaveReactedWith, toHaveStartedTyping,
and toHavePostedToChannel — covering the common Adapter mutation surface
that bot authors assert on. Each matcher's signature was checked against
packages/chat/src/types.ts, and each is paired with a smoke case that
drives a real Chat through the corresponding Thread/Channel API so
signature drift breaks the smoke test instead of silently agreeing with
the unit tests.
Emoji matching accepts both plain strings and EmojiValue ({ name }).
Text matching reuses the same extraction rules as toHavePosted —
strings, PostableMarkdown.markdown, PostableRaw.raw, and
PostableCard.fallbackText. Documented in matcher JSDoc, README, and
the Testing docs page.
---------
Co-authored-by: Ben Sabic <bensabic@users.noreply.github.com>
@chat-adapter/tests
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
| Matcher | Asserts |
|---|---|
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
Adapterimplementation routes webhooks through the rightChatInstance.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.