Skip to content
WebMCP Agents
Esc
↑↓navigate↵open⌘Jpreview
On this page

@webmcp-agents/testing

@webmcp-agents/testing tests WebMCP tools, confirmations, agents, the chat endpoint and privacy with Vitest: fixtures, worded matchers and model-free mocks.

@webmcp-agents/testing tests what this library builds, with Vitest and nothing hand-written: a tool in Node, a tool on the page with the cursor and a confirmation, an agent without a model, the /api/chat endpoint without a key, and where a secret does or does not end up. Fixtures arrive by destructuring (test.extend), matchers say what happened in words (expect.extend, typed), and mocks are vi.fn spies. The repository’s own tests use it.

pnpm add -D @webmcp-agents/testing vitest @vitest/browser-playwright
import { webAgentsProject } from "@webmcp-agents/testing/vitest";
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    projects: [webAgentsProject("unit"), webAgentsProject("browser")],
  },
});

*.test.* files run in Node, *.browser.test.* files in Chromium, where a test needs no setup or cleanup of its own. Vitest 4 or later is the one required peer; @webmcp-agents/chat, @webmcp-agents/telemetry, ai, react, react-dom, @playwright/test and @vitest/browser-playwright are optional, and an entry that needs a missing one fails with a sentence naming it.

Entries

Entry For Contents
@webmcp-agents/testing any test test, callTool, mockTool, the page helpers, expect, vi
@webmcp-agents/testing/vitest the config webAgentsProject
@webmcp-agents/testing/setup setupFiles the browser setup: polyfill, reduced motion, deterministic ids, cleanup and leak checks
@webmcp-agents/testing/matchers setupFiles the matchers alone (a page that must stay without WebMCP); webAgentsMatchers (every matcher, for your own expect.extend) and the WebAgentsMatchers type
@webmcp-agents/testing/chat agents, the chat mockAdapter, mockChatServer, renderChat
@webmcp-agents/testing/server /api/chat mockModel, chatRequest, readChatStream, withSession
@webmcp-agents/testing/telemetry spans memoryExporter, spanTree
@webmcp-agents/testing/playwright end to end openApp, runTool, listTools, parkedOn, confirm

Fixtures

Fixture Description
tools The page’s tools as an agent reaches them: call, list, mock, settled.
events Every AgentEvent from the start of the test.
user Trusted input: confirm, moveOn, scroll, click, type.
mount Mount HTML or a React element; removed after the test.
redactor A fresh page redactor, reset after the test.
runs Run events (0.5.0).
spans Every browser span of the test, in memory (needs @webmcp-agents/telemetry).

Matchers

Matcher Passes when
toBeToolResult The call succeeded (its text matches).
toBeToolError The call failed (with that code).
toHaveStructuredContent structuredContent has these values.
toHaveStarted A tool run started.
toHaveFilled The agent typed into a field.
toHaveParked A tool waited for the person.
toHaveYielded The cursor got out of the way.
toHaveEventOrder Events happened in this order.
toHaveCalledTool The run called a tool.
toHaveCalledTools The run called exactly these tools, in order.
toHaveAnswered The run answered this.
toHaveFinishedWith The run ended this way.
toHaveBeenOfferedTools The model was offered these tools.
toContainSensitive A secret is in this sink (use it negated).
toHaveSpan, toBeOneTrace A span by name and attributes; one trace with one root.
toHaveNoHardcodedText The rendered chat shows no string outside its catalog.

See also

Was this page helpful?