Skip to content

High-Level API Overview

The high-level layer is what most tests use. It models Telegram's domain directly: users, groups, channels, private chats — each as an actor object you control.

Two-layer design

┌───────────────────────────────────────────────────────────┐
│  High-Level Layer   grammy-testing                     │
│                                                           │
│  Chats (orchestrator)                                     │
│   ├─ newUser()   → User      → .replies (RepliesInbox)    │
│   │                          → .edits   (EditsLog)        │
│   │                          → .actions (ActionsLog)      │
│   ├─ newGroup()  → Group     → .messages (MessagesLog)    │
│   │               Supergroup → .deletions (DeletionsLog)  │
│   ├─ newChannel() → Channel  → .messages (MessagesLog)    │
│   └─ newPrivateChat() → PrivateChat → .messages           │
│                                                           │
│  Low-Level Layer   grammy-testing/low-level            │
│   ├─ OutgoingRequests  (raw capture)                      │
│   ├─ Mock update builders                                 │
│   └─ Session / state mocking                              │
└───────────────────────────────────────────────────────────┘

When to use which layer

SituationUse
Writing feature tests for handlersHigh-level (User, Group, etc.)
Asserting exact API call payloadLow-level (chats.outgoing)
Simulating API errorsLow-level (failNext, failAll)
Testing unusual update shapesLow-level update builders
Mocking session/stateLow-level (mockSession, mockState)

The Chats orchestrator

Everything starts with chats, returned by prepareBot:

ts
const { chats } = await prepareBot(bot);

chats is the central hub. It:

  • Creates actorsnewUser(), newGroup(), newSupergroup(), newChannel(), etc.
  • Routes captured API calls — when your bot calls sendMessage, chats routes the captured reply to the right user's replies inbox and the right chat's messages log.
  • Exposes cross-cutting accessorsrepliesFor(user), actionsFor(user), editsFor(user), deletionsFor(chat).
  • Provides idle() — await all in-flight fire-and-forget API calls.

Choosing the right page

I want to…Go to
Send messages as a userUser
Test membership in a groupGroups
Post to a channelChannel
Test DM between bot and userPrivateChat
Test Telegram Business APIBusinessAccount
Inspect bot repliesReply
Understand all log typesLogs
Create actors and manage stateChats

Released under the MIT License.