anchor-litesvm
Simplified Anchor testing with LiteSVM - Similar syntax to anchor-client, 78% less code, no mock RPC needed.
Overview
anchor-litesvm provides a streamlined testing experience for Anchor programs. It combines the familiar syntax of anchor-client with the speed of LiteSVM, plus comprehensive testing utilities.
Key Benefits:
- 78% less code compared to raw LiteSVM
- 40% faster compilation than anchor-client (no network dependencies)
- No mock RPC - zero configuration needed
- Familiar syntax - similar to anchor-client, transferable knowledge
Installation
[]
= "0.4"
Quick Start
use AnchorLiteSVM;
use ;
use Signer;
// Generate client types from your program
declare_program!;
Why anchor-litesvm?
| Feature | anchor-client + LiteSVM | anchor-litesvm |
|---|---|---|
| Lines of Code | 279 lines | 106 lines |
| Compilation | Slow (network deps) | 40% faster |
| Setup | Mock RPC needed | No config |
| Token Operations | Manual (30+ lines) | 1 line |
| Syntax | anchor-client | Similar |
No More Account Ordering Bugs
The #1 pain point in Solana testing - eliminated:
// Raw LiteSVM - order matters, easy to get wrong
let instruction = Instruction ;
// anchor-litesvm - named fields, order doesn't matter
let ix = ctx.program
.accounts
.args
.instruction?;
Features
Simplified Instruction Building
let ix = ctx.program
.accounts
.args
.instruction?;
ctx.execute_instruction?.assert_success;
Anchor Account Deserialization
// Deserialize with discriminator check
let account: MyAccount = ctx.get_account?;
// Deserialize without check (for PDAs with custom layouts)
let account: MyAccount = ctx.get_account_unchecked?;
Event Parsing
use EventHelpers;
let result = ctx.execute_instruction?;
// Parse all events of a type
let events: = result.parse_events?;
// Parse first event
let event: TransferEvent = result.parse_event?;
// Check if event was emitted
assert!;
result.;
All litesvm-utils Features Included
Since anchor-litesvm builds on litesvm-utils, you get all utilities:
// Account creation
let user = ctx.svm.create_funded_account?;
// Token operations
let mint = ctx.svm.create_token_mint?;
let ata = ctx.svm.create_associated_token_account?;
ctx.svm.mint_to?;
// Assertions
ctx.svm.assert_token_balance;
ctx.svm.assert_account_exists;
// PDA derivation
let = ctx.svm.get_pda_with_bump;
// Transaction analysis
result.print_logs;
let cu = result.compute_units;
Common Patterns
Token Testing
let mint = ctx.svm.create_token_mint?;
let token_account = ctx.svm.create_associated_token_account?;
ctx.svm.mint_to?;
// After your instruction
ctx.svm.assert_token_balance;
PDA Usage
let = ctx.svm.get_pda_with_bump;
let ix = ctx.program
.accounts
.args
.instruction?;
Error Testing
let result = ctx.execute_instruction?;
if !result.is_success
// Or assert specific errors
result.assert_error;
result.assert_anchor_error;
Testing
Related Crates
litesvm-utils- Framework-agnostic utilities (included)litesvm- The underlying fast Solana VManchor-lang- Anchor framework
License
MIT License - see LICENSE for details.