@beeman/testcontainers

中文文档
1.2.3发布于 2026/05/25MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2633

A collection of Testcontainers for testing your applications with real services.

原始使用文档
文档目录10 节
  1. Installation
  2. Available Containers
  3. LibSQL Server
  4. Solana Test Validator
  5. Surfpool
  6. Utilities
  7. createLocalSolanaClient
  8. Development
  9. Commands
  10. License

@beeman/testcontainers

A collection of Testcontainers for testing your applications with real services.

Installation

Install the package and Testcontainers:

bun add @beeman/testcontainers testcontainers

Available Containers

LibSQL Server

The LibsqlServerContainer provides a real libSQL server running in a Docker container. It uses the ghcr.io/beeman/libsql-server-healthcheck image by default so Testcontainers can wait on the container health check instead of log output.

Usage

If you want to use the example client below, also install @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()

Properties

  • container.url: Returns the HTTP URL (e.g., http://localhost:32768).
  • container.urlGrpc: Returns the gRPC URL (e.g., http://localhost:32769).
  • container.port: Returns the mapped HTTP port.
  • container.portGrpc: Returns the mapped gRPC port.

For advanced libSQL configuration such as authentication, replicas, or persistent storage, use the inherited GenericContainer methods from Testcontainers.

Solana Test Validator

The SolanaTestValidatorContainer provides a real Solana validator running in a Docker container, perfect for integration tests. It uses the beeman/solana-test-validator image by default.

Usage

If you want to use the createLocalSolanaClient helper below, see the optional install in the createLocalSolanaClient section.

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()

Properties

  • container.url: Returns the RPC URL (e.g., http://localhost:32768).
  • container.urlWs: Returns the WebSocket URL (e.g., ws://localhost:32769).
  • container.port: Returns the mapped RPC port.
  • container.portWs: Returns the mapped WebSocket port.

Surfpool

The SurfpoolContainer provides a Surfpool Solana simulator running in a Docker container. It runs in offline mode by default for isolated testing. It uses the surfpool/surfpool:latest image by default.

Basic Usage

If you want to use the createLocalSolanaClient helper below, see the optional install in the createLocalSolanaClient section.

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()

Network Selection

Connect to a specific Solana network (disables offline mode):

// 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()

Airdrop Configuration

Pre-fund accounts with SOL on startup:

const container = await new SurfpoolContainer()
  .withAirdrop(
    ['5cQvx...', '5cQvy...'],  // List of pubkeys to fund
    100_000_000_000             // Amount in lamports (100 SOL)
  )
  .start()

Custom Slot Time

Adjust the slot time for faster or slower block production:

// Faster slots (100ms instead of default 400ms)
const container = await new SurfpoolContainer()
  .withSlotTime(100)
  .start()

Block Production Mode

Control how blocks are produced:

// Transaction-based block production (new block per transaction)
const container = await new SurfpoolContainer()
  .withBlockProductionMode('transaction')
  .start()

Disable Features

const container = await new SurfpoolContainer()
  .withNoStudio()   // Disable the Studio UI
  .withNoDeploy()   // Disable auto deployments
  .start()

Full Configuration Example

// 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()

Properties

  • container.url: Returns the RPC URL (e.g., http://localhost:32768).
  • container.urlWs: Returns the WebSocket URL (e.g., ws://localhost:32769).
  • container.urlStudio: Returns the Studio URL (e.g., http://localhost:32770).
  • container.port: Returns the mapped RPC port.
  • container.portWs: Returns the mapped WebSocket port.
  • container.portStudio: Returns the mapped Studio port.

Configuration Methods

MethodDescription
withSolanaNetwork(network)Connect to 'mainnet', 'devnet', or 'testnet' (disables offline mode)
withRpcUrl(url)Use a custom RPC URL (disables offline mode)
withOffline(false)Disable offline mode (enabled by default)
withAirdrop(addresses, amount?)Pre-fund accounts (default: 10,000 SOL)
withAirdropKeypairPaths(paths)Fund accounts from keypair files
withSlotTime(ms)Set slot duration (default: 400ms)
withBlockProductionMode(mode)'clock' or 'transaction' (default: clock)
withNoStudio()Disable Studio UI
withNoDeploy()Disable auto deployments
withLogLevel(level)Set log level: 'trace', 'debug', 'info', 'warn', 'error', 'none'
withPort(port)Set RPC port (default: 8899)
withWsPort(port)Set WebSocket port (default: 8900)
withStudioPort(port)Set Studio port (default: 18488)

Utilities

createLocalSolanaClient

A convenience helper that creates a Solana RPC client configured for a local container. It wraps createDefaultLocalhostRpcClient from @solana/kit-plugins and works with both SolanaTestValidatorContainer and SurfpoolContainer.

To use this helper, install @solana/kit and @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 })

The returned LocalSolanaClient provides access to rpc, rpcSubscriptions, and the payer signer.

Parameters

ParameterTypeDescription
containerStartedSolanaTestValidatorContainer | StartedSurfpoolContainerA started container instance
payerTransactionSigner (optional)A payer signer; auto-generated if not provided

Development

This project is built with Bun.

Commands

  • Build: bun run build
  • Test: bun test
  • Lint: bun run lint
  • Type Check: bun run check-types

License

MIT – see LICENSE.

版本与发布记录

@beeman/testcontainers 共发布 18 个版本,首次发布于 2026/01/23;过去 12 个月发布了 18 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.2.3latest2026/05/255 个月前
1.2.3-canary-20260524194444canary2026/05/255 个月前
1.2.22026/05/076 个月前
1.2.2-canary-202605061405362026/05/066 个月前
1.2.12026/04/286 个月前
1.2.1-canary-202604271938252026/04/286 个月前

关于 @beeman/testcontainers 的常见问题

@beeman/testcontainers 是什么?

A collection of Testcontainers for testing your applications with real services.

如何安装 @beeman/testcontainers?

在项目目录运行 npm install @beeman/testcontainers,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@solana/kit(^6)、@solana/kit-plugins(^0.3)、testcontainers(^11 || ^12)、typescript(^5 || ^6)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@beeman/testcontainers 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@beeman/testcontainers 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@beeman/testcontainers 可以商用吗?

@beeman/testcontainers 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@beeman/testcontainers 的下载量有多少?

根据 npm 官方下载统计,@beeman/testcontainers 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,633 次,日均约 88 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@beeman/testcontainers 还在维护吗?

最新版本 1.2.3 发布于 2026/05/25(5 个月前);至今共发布 18 个版本,其中过去 12 个月发布了 18 个;首次发布于 2026/01/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。