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

test

Vitest's test, extended with the kit's fixtures.

Vitest’s test, extended with the kit’s fixtures through test.extend: tools, events, user, mount, redactor, runs and spans. Vitest sets up only the fixtures a test destructures, so a Node test that destructures none pays for no browser fixture.

Import

import { expect, test } from "@webmcp-agents/testing";

Example

import { expect, test } from "@webmcp-agents/testing";
import { WebAgentsProvider } from "@webmcp-agents/react";

import { Checkout } from "./app/checkout";
import { orders } from "./app/orders";

test("waits for the person before placing the order", async ({
  tools,
  events,
  user,
  mount,
}) => {
  orders.clear();
  mount(
    <WebAgentsProvider>
      <Checkout />
    </WebAgentsProvider>,
  );
  await expect.poll(() => tools.list()).toContain("place_order");

  const call = tools.call("place_order");
  await expect.poll(() => events).toHaveParked("place_order");
  expect(orders.get()).toHaveLength(0);

  // A trusted click on the parked control.
  await user.confirm("Place order");
  expect(await call).toBeToolResult();
  expect(orders.get()).toHaveLength(1);
});

test("gives up when the person moves on", async ({
  tools,
  events,
  user,
  mount,
}) => {
  mount(
    <WebAgentsProvider>
      <Checkout />
    </WebAgentsProvider>,
  );
  await expect.poll(() => tools.list()).toContain("place_order");

  const call = tools.call("place_order");
  await expect.poll(() => events).toHaveParked();
  // A trusted click away from the control.
  await user.moveOn();
  expect(await call).toBeToolError("cancelled");
  // The cursor got out of the way of the person's pointer.
  expect(events).toHaveYielded("pointer");
});

Options

test(name, fn, options?), with every modifier of Vitest’s test (skip, only, each, fails…).

Name Type Default Description
name string required The test’s name.
fn (fixtures) => unknown required The test. Destructure the fixtures it needs: async ({ tools, events }) => ….
options number | TestOptions Vitest’s A timeout or Vitest’s test options, as for test.

Returns

A Vitest TestAPI with the fixtures typed (WebAgentsFixtures). The entry also re-exports expect, vi, describe and the hooks, and registers the matchers on import.

Notes

Fixtures must be destructured in the test’s first parameter (async ({ tools }) => …): that is how Vitest knows which to set up.

Vitest’s own functions

The entry re-exports expect, vi, describe, beforeAll, beforeEach, afterAll and afterEach from Vitest, so a test file imports everything from one place.

See also

Was this page helpful?