anchor-litesvm 0.2.0

Simplified Anchor testing with LiteSVM - similar syntax to anchor-client, 78% less code
Documentation

anchor-litesvm

Testing framework for Anchor programs using LiteSVM.

This crate provides a simplified syntax similar to anchor-client but without RPC overhead, achieving 78% code reduction compared to raw LiteSVM.

Why anchor-litesvm?

Feature anchor-client + LiteSVM anchor-litesvm
Code Lines 279 106 (78% less)
Compilation Slow (network deps) 40% faster
Setup Mock RPC needed One line
Syntax anchor-client Similar to anchor-client
Helpers Manual Built-in

Key Features

  • Simplified Syntax: Similar to anchor-client
  • No Mock RPC Setup: One-line initialization
  • Integrated Test Helpers: Token operations, assertions, event parsing
  • Familiar API: If you know anchor-client, you know this
  • Transferable Knowledge: Test skills apply to production
  • Type Safety: Compile-time validation with Anchor types

Quick Start

use anchor_litesvm::{AnchorLiteSVM, TestHelpers, AssertionHelpers};
use solana_sdk::signature::Signer;

// 1. Generate client types from your program
anchor_lang::declare_program!(my_program);

#[test]
fn test_my_program() {
    // 2. One-line setup (no mock RPC needed)
    let mut ctx = AnchorLiteSVM::build_with_program(
        my_program::ID,
        include_bytes!("../target/deploy/my_program.so"),
    );

    // 3. Create test accounts with helpers
    let user = ctx.svm.create_funded_account(10_000_000_000).unwrap();
    let mint = ctx.svm.create_token_mint(&user, 9).unwrap();

    // 4. Build instruction (simplified syntax - similar to anchor client)
    let ix = ctx.program()
        .accounts(my_program::client::accounts::Transfer {
            from: sender_account,
            to: recipient_account,
            authority: user.pubkey(),
            token_program: spl_token::id(),
        })
        .args(my_program::client::args::Transfer { amount: 100 })
        .instruction()?;

    // 5. Execute and verify
    ctx.execute_instruction(ix, &[&user])?.assert_success();
    ctx.svm.assert_token_balance(&recipient_account, 100);
}

Common Patterns

Token Operations

use litesvm_utils::TestHelpers;

let mint = ctx.svm.create_token_mint(&authority, 9)?;
let token_account = ctx.svm.create_associated_token_account(&mint.pubkey(), &owner)?;
ctx.svm.mint_to(&mint.pubkey(), &token_account, &authority, 1_000_000)?;

PDA Derivation

// Just the address
let pda = ctx.svm.get_pda(&[b"vault", user.pubkey().as_ref()], &program_id);

// With bump seed
let (pda, bump) = ctx.svm.get_pda_with_bump(&[b"vault"], &program_id);

Error Testing

let result = ctx.execute_instruction(ix, &[&user])?;
result.assert_failure();
result.assert_error("insufficient funds");
result.assert_error_code(6000); // Anchor custom error

Event Parsing

use anchor_litesvm::EventHelpers;

let events: Vec<TransferEvent> = result.parse_events()?;
result.assert_event_emitted::<TransferEvent>();

Account Deserialization

let account: MyAccountType = ctx.get_account(&pda)?;
assert_eq!(account.authority, user.pubkey());

Documentation

Modules

  • [account] - Account deserialization utilities
  • [builder] - Test environment builders
  • [context] - Main test context (AnchorContext)
  • [events] - Event parsing helpers
  • [instruction] - Instruction building utilities
  • [program] - Simplified Program API