Registry / testing / telegraf-test

telegraf-test

JSON →
library1.2.1jsnpmunverified

Telegraf Test is a simple test toolkit for Telegram bots that emulates the Telegram Bot API server locally, allowing offline testing without network calls. Current stable version is 1.2.1. It works with Telegraf versions below 3 and supports common test frameworks like Mocha. Key differentiators include local server emulation, support for sending messages, inline queries, and callback queries, and offline operation. It uses Axios to send requests to a local webhook endpoint.

npm install telegraf-test
INSTALL
IMPORT
SIG · TELEGRAF-TEST
T
telegraf-test
testingjavascriptv1.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TelegrafTest
const TelegrafTest = require('telegraf-test')
import TelegrafTest from 'telegraf-test'
Package uses CommonJS and does not support ESM imports
sendMessageWithText
test.sendMessageWithText('/ping')
test.sendMessageWithText({ text: '/ping' })
First argument is text string, not an object
sendCallbackQueryWithData
test.sendCallbackQueryWithData('data')
test.sendCallbackQueryWithData({ data: 'data' })
First argument is data string, not an object

Shows how to set up a Telegraf bot with webhook and test it using TelegrafTest by sending a message and verifying the reply.

const Telegraf = require('telegraf') const TelegrafTest = require('telegraf-test') const port = 3000 const secretPath = 'secret-path' const bot = new Telegraf('ABCD:1234567890') const test = new TelegrafTest({ url: `http://127.0.0.1:${port}/${secretPath}` }) test.setUser({ id: 1234, username: '@TiagoEDGE' }) bot.hears(/ping/i, ctx => { ctx.reply('Pong!') }) bot.startWebhook(`/${secretPath}`, null, port) test.sendMessageWithText('/ping') .then(res => { console.log(res.data) }) .catch(error => { console.error(error) })
Debug
Known issues
gotchaLibrary only supports Telegraf <3 (major version 2.x). Not compatible with Telegraf v4+.
fix
Use with Telegraf v2.x or consider upgrading to Telegraf v4+ and finding an alternative test toolkit.
affects: >=1.0.0
breakingPackage uses CommonJS only; ESM imports will fail.
fix
Use require() instead of import.
affects: >=1.0.0
gotchaDefault port for emulator is 2000, which may conflict with other services.
fix
Specify a different port in options: new TelegrafTest({ port: 3001 })
affects: >=1.0.0
gotchaThe 'sendUpdate' method returns false for ignored updates; developers often assume it always returns a response.
fix
Check for false return value: const res = await test.sendUpdate({...}); if (res === false) { /* handle ignored */ }
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: TelegrafTest is not a constructor
Using ESM import instead of CommonJS require, or incorrect import path
fix
Use const TelegrafTest = require('telegraf-test');
Error: listen EADDRINUSE :::2000
Default port 2000 already in use
fix
Pass a different port in options: new TelegrafTest({port: 3001})
Cannot find module 'telegraf'
Telegraf dependency not installed
fix
Run: npm install telegraf@2
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies
axiosrequiredUsed to send HTTP requests to the bot's webhook endpoint
Agent activity
12 hits · last 30 days
node
10
Resources
telegraf-test — npm install telegraf-test · libregistry