@ beeman/ 测试容器
收集资料 测试容器 用真实服务测试您的应用程序.
安装
安装软件包和测试容器 :
bun add @beeman/testcontainers testcontainers
可用集装箱
LibSQL 服务器
这个 LibsqlServerContainer 提供在 Docker 容器中运行的 libSQL 服务器 。 它使用 ghcr.io/beeman/libsql-server-healthcheck 默认的图像可以让 Testcontainers 等待容器健康检查而不是日志输出 .
使用量
如果您想要使用下面的示例客户端, 请同时安装 @libsql/client数字 :
bun add @libsql/client
import { createClient } from '@libsql/client'
import { LibsqlServerContainer } from '@beeman/testcontainers'
const container = await new LibsqlServerContainer().start()
const client = createClient({ url: container.url })
await client.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)')
await client.execute({
sql: 'INSERT INTO users(name) VALUES (?)',
args: ['beeman'],
})
const result = await client.execute('SELECT id, name FROM users')
console.log(result.rows[0]?.name)
client.close()
await container.stop()
属性
container.url: 返回 HTTP URL(例如,http://localhost:32768) (中文(简体) ).container.urlGrpc: 返回 gRPC URL(例如,http://localhost:32769) (中文(简体) ).container.port: 返回映射的 HTTP 端口.container.portGrpc: 返回已映射的 gRPC 端口.
对于高级的libSQL配置,如认证、复制或持续存储,使用所继承的 GenericContainer 来自测试容器的方法.
索拉纳测试验证器
这个 SolanaTestValidatorContainer 提供真实的索拉纳验证器在多克容器中运行,适合集成测试. 它使用 beeman/solana-test-validator 默认图像.
使用量
如果你想用这个 createLocalSolanaClient 下面的帮助,请参见可选安装 createLocalSolanaClient 节.
import { SolanaTestValidatorContainer, createLocalSolanaClient } from '@beeman/testcontainers'
// Start the container
const container = await new SolanaTestValidatorContainer().start()
// Create a client with auto-generated payer
const client = await createLocalSolanaClient({ container })
// ... run your tests ...
// Stop the container when done
await container.stop()
属性
container.url: 返回 RPC URL(例如,http://localhost:32768) (中文(简体) ).container.urlWs: 返回 WebSocket URL(例如,ws://localhost:32769) (中文(简体) ).container.port: 返回映射的 RPC 端口.container.portWs: 返回已映射的 WebSocket 端口.
冲浪池
这个 SurfpoolContainer 页:1 冲浪池 索拉纳模拟器在一个多克容器中运行. 它默认以离线模式运行,用于孤立测试. 它使用 surfpool/surfpool:latest 默认图像.
基本用途
如果你想用这个 createLocalSolanaClient 下面的帮助,请参见可选安装 createLocalSolanaClient 节.
import { SurfpoolContainer, createLocalSolanaClient } from '@beeman/testcontainers'
// Start the container (offline mode by default)
const container = await new SurfpoolContainer().start()
// Create a client with auto-generated payer
const client = await createLocalSolanaClient({ container })
// Access the Studio URL
const studioUrl = container.urlStudio
// ... run your tests ...
// Stop the container when done
await container.stop()
网络选择
连接到特定的索拉纳网络( 离线模式):
// Connect to devnet
const container = await new SurfpoolContainer()
.withSolanaNetwork('devnet')
.start()
// Or use a custom RPC URL
const container = await new SurfpoolContainer()
.withRpcUrl('https://my-rpc-provider.com')
.start()
空投配置
启动时SOL的基金前账户:
const container = await new SurfpoolContainer()
.withAirdrop(
['5cQvx...', '5cQvy...'], // List of pubkeys to fund
100_000_000_000 // Amount in lamports (100 SOL)
)
.start()
自定义时间
为更快或更慢的块生产调整槽时间 :
// Faster slots (100ms instead of default 400ms)
const container = await new SurfpoolContainer()
.withSlotTime(100)
.start()
块生产模式
控制块的生成方式 :
// Transaction-based block production (new block per transaction)
const container = await new SurfpoolContainer()
.withBlockProductionMode('transaction')
.start()
禁用特性
const container = await new SurfpoolContainer()
.withNoStudio() // Disable the Studio UI
.withNoDeploy() // Disable auto deployments
.start()
完整配置示例
// Connect to devnet with custom configuration
const container = await new SurfpoolContainer()
.withSolanaNetwork('devnet')
.withAirdrop(['5cQvx...'], 500_000_000_000)
.withSlotTime(200)
.withBlockProductionMode('transaction')
.withLogLevel('debug')
.start()
属性
container.url: 返回 RPC URL(例如,http://localhost:32768) (中文(简体) ).container.urlWs: 返回 WebSocket URL(例如,ws://localhost:32769) (中文(简体) ).container.urlStudio: 返回 Studio URL(例如,http://localhost:32770) (中文(简体) ).container.port: 返回映射的 RPC 端口.container.portWs: 返回已映射的 WebSocket 端口.container.portStudio: 返回映射工作室端口.
配置方法
| 方法 | 说明 |
|---|---|
withSolanaNetwork(network) | 连接到 'mainnet', (中文(简体) ), 'devnet',或 'testnet' (残疾离线模式) |
withRpcUrl(url) | 使用自定义的 RPC URL( 离线模式) |
withOffline(false) | 禁用离线模式( 默认启用) |
withAirdrop(addresses, amount?) | 基金前账户(违约:10 000 SOL) |
withAirdropKeypairPaths(paths) | 来自 keypair 文件的基金账户 |
withSlotTime(ms) | 设置空档持续时间( 默认: 400ms) |
withBlockProductionMode(mode) | 'clock' 或者说 'transaction' (默认:时钟) |
withNoStudio() | 禁用工作室用户界面 |
withNoDeploy() | 禁用自动部署 |
withLogLevel(level) | 设置日志级别 : 'trace', (中文(简体) ), 'debug', (中文(简体) ), 'info', (中文(简体) ), 'warn', (中文(简体) ), 'error', (中文(简体) ), 'none' |
withPort(port) | 设定 RPC 端口( 默认: 8899) |
withWsPort(port) | 设置 WebSocket 端口( 默认: 8900) |
withStudioPort(port) | 设置工作室端口( 默认: 18488) |
公用事业
createLocalSolanaClient
一个创建 Solana RPC 客户端为本地容器配置的便利助手 。 包起来 createDefaultLocalhostRpcClient 从 @solana/kit-plugins 并且与双方合作 SolanaTestValidatorContainer 财务报告和审定财务报表 SurfpoolContainer。 。 。 .
要使用此帮助, 请安装 @solana/kit 财务报告和审定财务报表 @solana/kit-plugins数字 :
bun add @solana/kit @solana/kit-plugins
import { SurfpoolContainer, createLocalSolanaClient } from '@beeman/testcontainers'
const container = await new SurfpoolContainer().start()
// Create a client with an auto-generated payer
const client = await createLocalSolanaClient({ container })
// Or provide your own payer
import { generateKeyPairSigner } from '@solana/kit'
const payer = await generateKeyPairSigner()
const client = await createLocalSolanaClient({ container, payer })
回来的 LocalSolanaClient 提供访问 rpc, (中文(简体) ), rpcSubscriptions,则 payer 签名者.
参数
| 参数 | 类型 | 说明 |
|---|---|---|
container | StartedSolanaTestValidatorContainer | StartedSurfpoolContainer | 一个开始的容器实例 |
payer | TransactionSigner (可选) | 付款人签名人; 自动生成( 如果不提供) |
发展
这个项目是用 阿宝。 。 。 .
图标
- 构建数字 :
bun run build - 测试数字 :
bun test - 林特数字 :
bun run lint - 类型检查数字 :
bun run check-types
许可证
麻省理工学院 — 看 自由主义。 。 。 .