---
slug: reference/testing
description: "@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`

`@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.

```sh
pnpm add -D @webmcp-agents/testing vitest @vitest/browser-playwright
```

```ts title="vitest.config.ts"
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`](/docs/reference/testing/test), [`callTool`](/docs/reference/testing/call-tool), [`mockTool`](/docs/reference/testing/mock-tool), [the page helpers](/docs/reference/testing/helpers), `expect`, `vi` |
| `@webmcp-agents/testing/vitest` | the config | [`webAgentsProject`](/docs/reference/testing/web-agents-project) |
| `@webmcp-agents/testing/setup` | `setupFiles` | [the browser setup](/docs/reference/testing/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`](/docs/reference/testing/mock-adapter), [`mockChatServer`](/docs/reference/testing/mock-chat-server), [`renderChat`](/docs/reference/testing/render-chat) |
| `@webmcp-agents/testing/server` | `/api/chat` | [`mockModel`](/docs/reference/testing/mock-model), [`chatRequest`, `readChatStream`, `withSession`](/docs/reference/testing/server-requests) |
| `@webmcp-agents/testing/telemetry` | spans | [`memoryExporter`, `spanTree`](/docs/reference/testing/telemetry) |
| `@webmcp-agents/testing/playwright` | end to end | [`openApp`, `runTool`, `listTools`, `parkedOn`, `confirm`](/docs/reference/testing/playwright) |

## Fixtures

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

## Matchers

| Matcher | Passes when |
|---|---|
| [`toBeToolResult`](/docs/reference/testing/to-be-tool-result) | The call succeeded (its text matches). |
| [`toBeToolError`](/docs/reference/testing/to-be-tool-error) | The call failed (with that code). |
| [`toHaveStructuredContent`](/docs/reference/testing/to-have-structured-content) | `structuredContent` has these values. |
| [`toHaveStarted`](/docs/reference/testing/to-have-started) | A tool run started. |
| [`toHaveFilled`](/docs/reference/testing/to-have-filled) | The agent typed into a field. |
| [`toHaveParked`](/docs/reference/testing/to-have-parked) | A tool waited for the person. |
| [`toHaveYielded`](/docs/reference/testing/to-have-yielded) | The cursor got out of the way. |
| [`toHaveEventOrder`](/docs/reference/testing/to-have-event-order) | Events happened in this order. |
| [`toHaveCalledTool`](/docs/reference/testing/to-have-called-tool) | The run called a tool. |
| [`toHaveCalledTools`](/docs/reference/testing/to-have-called-tools) | The run called exactly these tools, in order. |
| [`toHaveAnswered`](/docs/reference/testing/to-have-answered) | The run answered this. |
| [`toHaveFinishedWith`](/docs/reference/testing/to-have-finished-with) | The run ended this way. |
| [`toHaveBeenOfferedTools`](/docs/reference/testing/to-have-been-offered-tools) | The model was offered these tools. |
| [`toContainSensitive`](/docs/reference/testing/to-contain-sensitive) | A secret is in this sink (use it negated). |
| [`toHaveSpan`](/docs/reference/testing/to-have-span), [`toBeOneTrace`](/docs/reference/testing/to-be-one-trace) | A span by name and attributes; one trace with one root. |
| [`toHaveNoHardcodedText`](/docs/reference/testing/to-have-no-hardcoded-text) | The rendered chat shows no string outside its catalog. |

## See also

- [Test what you build](/docs/testing): the guide, one page per layer
