Skip to content

Testing

discord0/testing runs your app the way Discord would, with no Discord and no network. It builds interaction payloads, sends them to the app, and records what the app sent back: the inline response, deferrals, edits, and follow-ups. REST calls go to a fake that answers like Discord.

The helpers return plain data, so any test runner works. The starter’s test uses Bun’s:

src/index.test.ts
import { expect, test } from "bun:test";
import { createTestClient } from "discord0/testing";
import app from "./index";
const client = createTestClient(app);
test("/ping replies with Pong!", async () => {
const result = await client.command("ping");
expect(result.reply).toEqual({ content: "Pong!" });
});

Run it with npm test, or discord0 test, which runs bun test on the Bun that the discord0 CLI runs on, so the project needs no Bun install of its own. Arguments pass through, as in discord0 test --watch.

discord0/testing is a separate entry point, so production bundles leave it out as long as only tests import it.

Method Sends
client.command(path, values?, init?) A slash command. Name a subcommand by its path, as "ticket open".
client.component(customId, init?) A button click, or a select menu choice with init.values.
client.modal(customId, fields?, init?) A modal submission. fields maps text input IDs to their values.
client.autocomplete(path, values, focused, init?) Autocomplete while the user types into the focused option.
client.runTask(name, options?) Runs a scheduled task.

Option values are what the user typed or picked. Pass strings, numbers, and booleans, or IDs for users, channels, roles, and attachments. The client reads each option’s type from the app’s command definitions. A misspelled command, subcommand, or option rejects with INVALID_TEST_INPUT, so the test fails:

await client.command("roll", { sides: 20 });
await client.component("approve:42");
await client.component("color", { values: ["red"] });
await client.modal("feedback", { comment: "Great bot" });
await client.autocomplete("search", { query: "ap" }, "query");

init sets who sends the interaction and where. It takes user fields, guildId for a server interaction (the user becomes a member), member, channelId, locale, guildLocale, applicationId, entitlements, and appPermissions. For client.component(), it also takes values, componentType, and the message the component is on. For client.modal(), it also takes message. Pass defaults for every interaction with createTestClient(app, { interaction: { guildId } }).

Each method resolves once the handler finishes, including work after the response, such as edits after a deferral.

By default the client signs each payload with a test key and sends it through app.fetch, as Discord would, so signature checks and payload validation run too. The client points the app at the test key, so no DISCORD_PUBLIC_KEY is needed.

createTestClient(app, { transport: "dispatch" }) hands payloads straight to app.dispatch instead. client.send(payload) sends a payload with the client’s transport, and client.fetch(payload) and client.dispatch(payload) pick a transport for one payload.

The builders behind the methods are exported too, for payloads you want to change first:

import { buildCommand, createTestClient } from "discord0/testing";
const client = createTestClient(app);
const interaction = buildCommand(app, "ping");
const user = interaction.user!;
interaction.data.resolved = { users: { [user.id]: user } };
const result = await client.send(interaction);

buildCommand, buildComponent, buildModalSubmit, and buildAutocomplete return discord-api-types interactions with fresh IDs and tokens. testMessage() builds a message for buildComponent(id, { message }), and testIds holds the default application, user, channel, and server IDs.

An interaction resolves to an InteractionResult:

Field Description
interaction The interaction payload that was sent.
reply The message the user sees: the inline reply or update with every edit applied. undefined if there is none or it was deleted.
response The inline interaction response, as the app returned it.
deferred Whether the response was a deferral, by the handler or after autoDeferAfterMs.
ephemeral Whether the reply is visible only to the user.
edits Bodies of c.editReply() calls, including a reply sent after a deferral.
followUps Bodies of c.followUp() calls.
choices Autocomplete choices, for an autocomplete response.
modal The modal shown, if the response opened one.
calls REST calls made with this interaction’s token: method, path, query, body, and files.
errors Errors the app reported, such as a handler throwing.
status The HTTP status from app.fetch. Through dispatch, 200, or 500 if the app produced no response.

A handler that throws still produces a result. reply is the ephemeral error message the user would see (messages.error if you set one), and errors holds what was thrown. The app’s onError runs as usual, with the interaction and how long handling took.

client.runTask() resolves to the messages the task posted with c.send(), each with its channelId and data, and the run’s REST calls. It rejects if the task throws, as app.runTask() does:

const result = await client.runTask("standup", {
scheduledTime: new Date("2026-11-23T12:45:00Z"),
});
expect(result.messages).toEqual([
{ channelId: "123", data: { content: "Standup in 15 minutes!" } },
]);

client.calls lists every REST call through the client, in order, including calls that belong to no single interaction or task run.

The fake answers the calls the framework makes. It sends, edits, reads, and deletes replies and follow-ups, posts channel messages, and executes Discord webhooks. It returns messages built from what was sent. Any other call fails with an error that names it.

Pass rest to answer calls yourself, such as to make one fail. Return a Response to send it as is, any other value to send it as JSON, or undefined to use the default fake:

const client = createTestClient(app, {
rest: (call) =>
call.path.startsWith("/channels/")
? Response.json(
{ code: 50001, message: "Missing Access" },
{ status: 403 },
)
: undefined,
});

client.sign(body, { path }) returns a request signed with the test key, for endpoints the helpers don’t cover, such as webhook events. Pass a waitUntil to app.fetch to wait for the event handlers:

const pending: Promise<void>[] = [];
const request = await client.sign(eventBody, { path: "/events" });
const response = await app.fetch(request, {
waitUntil: (promise) => void pending.push(promise),
});
await Promise.all(pending);

The client attaches to the app the first time it sends something, replacing its REST client and public key, and every send attaches it again. Create one client per app, such as at the top of a test file, rather than several used at once.