Expand description
§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, 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
Re-exports§
pub use account::get_anchor_account;pub use account::get_anchor_account_unchecked;pub use account::AccountError;pub use builder::AnchorLiteSVM;pub use builder::ProgramTestExt;pub use context::AnchorContext;pub use events::parse_event_data;pub use events::EventError;pub use events::EventHelpers;pub use instruction::build_anchor_instruction;pub use instruction::calculate_anchor_discriminator;pub use program::InstructionBuilder;pub use program::Program;
Modules§
- account
- builder
- Builder pattern for setting up Anchor test environments
- context
- events
- Event parsing and assertion utilities for Anchor programs
- instruction
- program
- Simplified instruction builder for LiteSVM testing without RPC overhead.
Structs§
- Account
Meta - Describes a single account read or written by a program during instruction execution.
- Instruction
- A directive for a single invocation of a Solana program.
- Keypair
- A vanilla Ed25519 key pair
- LiteSVM
- LiteSVM
Builder - Builder for creating a LiteSVM instance with programs pre-deployed
- Pubkey
- The address of a Solana account.
- Transaction
Result - Wrapper around LiteSVM’s TransactionMetadata with helper methods for testing
Enums§
Traits§
- Account
Deserialize - A data structure that can be deserialized and stored into account storage,
i.e. an
AccountInfo’s mutable data slice. - Anchor
Serialize - A data-structure that can be serialized into binary format by NBOR.
- Assertion
Helpers - Assertion helper methods for LiteSVM
- Signer
- The
Signertrait declares operations that all digital signature providers must support. It is the primary interface by which signers are specified inTransactionsigning interfaces - Test
Helpers - Test helper methods for LiteSVM
- Transaction
Helpers - Transaction helper methods for LiteSVM