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 ;
use Signer;
// 1. Generate client types from your program
declare_program!;
Common Patterns
Token Operations
use TestHelpers;
let mint = ctx.svm.create_token_mint?;
let token_account = ctx.svm.create_associated_token_account?;
ctx.svm.mint_to?;
PDA Derivation
// Just the address
let pda = ctx.svm.get_pda;
// With bump seed
let = ctx.svm.get_pda_with_bump;
Error Testing
let result = ctx.execute_instruction?;
result.assert_failure;
result.assert_error;
result.assert_error_code; // Anchor custom error
Event Parsing
use EventHelpers;
let events: = result.parse_events?;
result.;
Account Deserialization
let account: MyAccountType = ctx.get_account?;
assert_eq!;
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