Docs menu

Test API

Vitest plugin

The options of channelstack() from channelstack/vitest, what the plugin adds to a Vitest run, and the fixtures it gives every test. For a walkthrough, see Writing tests.

import { defineConfig } from "vitest/config";
import channelstack from "channelstack/vitest";

export default defineConfig({
  plugins: [channelstack({ mode: "auto", timeout: "10s", quietFor: "750ms" })],
});

Signature#

import channelstack from "channelstack/vitest";

function channelstack(options?: ChannelstackIntegrationOptions): Plugin;
RequirementValue
Packagechannelstack (npm i -D channelstack)
ImportDefault export of channelstack/vitest
Vitest>=5.0.0 <6 (an optional peer dependency)
Node>=22

Options#

interface ChannelstackIntegrationOptions {
  server?: string;
  mode?: "auto" | "attach" | "start";
  timeout?: Duration;
  quietFor?: Duration;
}
OptionTypeDefaultMeaning
serverstringCHANNELSTACK_URL, else the URL in <home>/server.jsonAttach to the server at this URL. Its token comes from CHANNELSTACK_TOKEN, or from server.json when the URLs match
mode"auto" | "attach" | "start""auto"How the run finds its server. See Modes
timeoutDuration"10s"Default timeout of every matcher, of channelstack.quiet(), and of every SDK wait
quietForDuration"750ms"Default quiet window of negative and exact-count matchers, of channelstack.quiet(), and of the SDK's latest… reads

A Duration is milliseconds as a number, or a string such as "250ms", "3s", "1m".

timeout and quietFor are the defaults of the SDK's own waits too (waitForReply, waitForComment, latestReply, settled, …): the setup file sets them in every worker. A call that passes its own timeout or quietFor wins.

A project that sets no testTimeout gets one from the plugin: 30 seconds, or three times timeout if that's longer, so a failing check can report its own message before Vitest's 5-second limit stops the test. A project's own testTimeout, and Vitest's --testTimeout flag, always win.

Modes#

ModeServer runningNo server running
autoAttaches to itStarts a private one for the run
attachAttaches to itFails the run: No running channelstack server found. Start one with `npx channelstack`, or set CHANNELSTACK_URL and CHANNELSTACK_TOKEN.
startIgnores it, starts a private oneStarts a private one for the run

"Running" means a server at server (or CHANNELSTACK_URL, or the one in server.json) answers GET /_channelstack/status with the token within 2 seconds.

A private server is the binary of the installed platform package (@channelstack/server-<platform>-<arch>), or CHANNELSTACK_SERVER_BIN. It runs as channelstack start --no-portless --port 0 --server-file <temporary file>, so it uses a free loopback port, no *.localhost names, and never touches the shared server.json. It inherits the test run's environment, CHANNELSTACK_<CHANNEL>_WEBHOOK_URL and live credentials included. It has 15 seconds to become ready, and stops when the run ends.

What the plugin adds#

PieceWhat it does
Global setupRuns once per run: attaches to or starts the server (by mode), and hands its URL, its token, timeout and quietFor to the test workers through Vitest's provide. Stops a server it started when the run ends
Setup fileRuns in every worker, before the project's own setupFiles: one client per worker, one session per test, the fixtures below, the matchers registered with expect.extend, and timeout and quietFor as the SDK's wait defaults
TypesAugments vitest: TestContext gets the fixtures, Matchers gets every matcher, ProvidedContext gets channelstack
test.envSets CHANNELSTACK_VITEST_OPTIONS to the plugin's options as JSON, for the global setup to read
test.testTimeoutSet when the project sets none: 30 seconds, or three times timeout

The setup file comes first in setupFiles, so its beforeEach work (opening the session, the fixtures) runs before the project's hooks, and its cleanup runs after them. A project's own hooks can use the fixtures.

The token goes to the workers directly. Nothing a test spawns inherits it.

Sessions#

Every test gets its own session in the server's shared world. The session opens before the test's hooks run and closes after them, whether or not the test names a fixture. Closing it removes everything the test created: people, channels, repositories, teams, pages, faults. Disposing never fails a test.

Relative fixture paths in a test (files: "./fixtures/web", content: { path: "./notes.md" }) resolve from the test file's directory.

Fixtures#

Every fixture is automatic. Destructure what a test needs:

import { expect, test } from "vitest";test("answers in the thread", async ({ slack, channelstack }) => {  const ana = await slack.createUser({ name: "Ana" });  const channel = await slack.createChannel({ participants: [ana, slack.app] });  const msg = await ana.say("@my-agent hi", { channel });  await expect(msg.thread).toHaveReplyFrom("my-agent");  await channelstack.quiet();});
FixtureTypeWhat it is
channelstackChannelstackFixtureThe server itself. See below
slackSlackFixtureThe test's view of the fake (or live) Slack workspace
githubGitHubFixtureThe test's view of the GitHub organization
linearLinearFixtureThe test's view of the Linear workspace
notionNotionFixtureThe test's view of the Notion workspace
resendResendFixtureThe test's view of the Resend team
googleGoogleFixtureThe test's view of the Gmail mailbox
discordDiscordFixtureThe test's view of the Discord guild
openaiOpenAIFixtureThe fake OpenAI, your agent's model

The channel fixtures' members are listed in the API reference: SlackFixture, GitHubFixture, LinearFixture, NotionFixture, ResendFixture, GoogleFixture, DiscordFixture, and the model's: OpenAIFixture.

A suite builds its own fixtures on these with test.extend:

import type { SlackConversation, SlackUser } from "channelstack";import { test as base } from "vitest";export const test = base.extend<{ ana: SlackUser; support: SlackConversation }>({  ana: async ({ slack }, use) => use(await slack.createUser({ name: "Ana" })),  support: async ({ slack, ana }, use) =>    use(await slack.createChannel({ name: "support", participants: [ana, slack.app] })),});

ChannelstackFixture#

MemberSignatureReturnsNotes
urlreadonly url: stringstringControl API and UI base, e.g. https://channelstack.localhost
envenv(): Promise<Record<string, string>>Env vars for the appWhat channelstack env prints: fake tokens, signing secrets, API URLs
quietquiet(options?: { quietFor?: Duration; timeout?: Duration }): Promise<void>Resolves when the app is quiet in this test's sessionQuiet: no call in flight, no delivery in flight or about to be retried, no open stream, for quietFor. Defaults: the plugin's quietFor and timeout
resetreset(): Promise<void>voidDrops all state on every channel, for every session. Avoid it in parallel suites. Refused with unsupported_in_live when a channel is live; that refusal fails the test, since it belongs to no session

Live refusals skip tests#

When a channel runs live, some operations can't run there. The plugin turns two refusals into a skipped test, with the reason, instead of a failure:

Error codeMeansOutcome
unsupported_in_liveThe operation has no live counterpart, or not yet (reading or forging a delivery, uninstallApp)Test skipped: live: <message>
action_requiresIt needs a piece live mode doesn't drive yet, whatever the run has: the browser actor beyond typing and clicking (#47), or proxy mode (#24). Also a person with no saved browser sessionTest skipped: live: <message>
Any other codelive_misconfigured, live_not_allowlisted, fake_actor_in_live_world, a platform error, …Test fails

The skip is per test: the refused request names its session, so concurrent tests skip only themselves. A fake run never sees these refusals, so every test runs in full on every fake run.

A channel fixture that is refused while it is set up (its live backend isn't built, or its credentials are set in part) doesn't touch the other channels' tests. The refusal is kept and raised the first time the test uses that fixture: it skips the test for the two codes above and fails it for any other.

The matchers' failure messages add what the app left open ("Still open: …"). That read never turns a failing test into a skipped one.

See Live mode and the error codes.

Suggest changes to this page