Getting Started
Install LiteSVM and write your first tests.
Quick Start Guide
This guide covers the basics: how to create an account, fund an account, and send a transaction.
Install Dependencies
Add LiteSVM to your dev dependencies:
cargo add --dev litesvmSeveral Solana crates enhance the testing experience by providing essential types and utilities, in this case, we will need solana-sdk.
cargo add --dev solana-sdkpnpm add @solana/kit @solana/kit-plugins @solana-program/systemnpm install @solana/kit @solana/kit-plugins @solana-program/systemyarn add @solana/kit @solana/kit-plugins @solana-program/systembun add @solana/kit @solana/kit-plugins @solana-program/systemCreate Test File
Inside your workspace, create a folder for tests.
mkdir testsCreate a new TypeScript file for your test.
touch basic-test.tsCreate and Fund an Account
Let's write your first LiteSVM test.
Inside your tests folder, create a new file and copy this code:
use litesvm::LiteSVM;
use solana_sdk::signature::{Keypair, Signer};
#[test]
fn create_account() {
// Create the test environment
let mut svm = LiteSVM::new();
// Create a test account
let user = Keypair::new();
// Fund the account with SOL
svm.airdrop(&user.pubkey(), 1_000_000_000).unwrap();
// Check the balance
let balance = svm.get_balance(&user.pubkey()).unwrap();
assert_eq!(balance, 1_000_000_000);
println!("Account funded with {} SOL", balance as f64 / 1e9);
}Copy this code into your test file:
import { createEmptyClient, lamports } from '@solana/kit';
import { litesvm, payer } from '@solana/kit-plugins';
// Create a client with LiteSVM and your payer
const client = createEmptyClient()
.use(litesvm())
.use(payer(mySigner));
// Fund the account with SOL
client.svm.airdrop(client.payer.address, lamports(5_000_000_000n));
// Check the balance
const balance = client.svm.getBalance(client.payer.address);
console.log('Balance:', balance, 'lamports');Run Your Test
cargo test create_account -- --show-outputYou should see:
running 1 test
test create_account ... ok
successes:
---- create_account stdout ----
Account funded with 1 SOL
successes:
create_accountnpx tsx basic-test.tsYou should see:
Balance: 5000000000n lamportsThat's it! You just made your first litesvm test, that covers creating an account and funding it.
Execute a Transaction
Now let's create a transfer transaction.
Create a new test file and copy this code:
use litesvm::LiteSVM;
use solana_sdk::{
signature::{Keypair, Signer},
system_instruction,
transaction::Transaction,
};
#[test]
fn test_transfer() {
let mut svm = LiteSVM::new();
// Create two accounts
let alice = Keypair::new();
let bob = Keypair::new();
// Fund Alice
svm.airdrop(&alice.pubkey(), 2_000_000_000).unwrap();
// Create transfer instruction
let transfer = system_instruction::transfer(
&alice.pubkey(),
&bob.pubkey(),
1_000_000_000, // 1 SOL
);
// Build and sign transaction
let tx = Transaction::new_signed_with_payer(
&[transfer],
Some(&alice.pubkey()),
&[&alice],
svm.latest_blockhash(),
);
// Send it (execution happens immediately)
svm.send_transaction(tx).unwrap();
// Check new balances
assert_eq!(svm.get_balance(&bob.pubkey()).unwrap(), 1_000_000_000);
assert!(svm.get_balance(&alice.pubkey()).unwrap() < 1_000_000_000);
println!("Transfer successful!");
}Create a new file and copy this code:
import {
appendTransactionMessageInstruction,
createEmptyClient,
createTransactionMessage,
lamports,
pipe,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners,
} from '@solana/kit';
import { litesvm, payer } from '@solana/kit-plugins';
import { getTransferSolInstruction } from '@solana-program/system';
// Create client with LiteSVM and payer
const client = createEmptyClient()
.use(litesvm())
.use(payer(mySigner));
// Fund the payer
client.svm.airdrop(client.payer.address, lamports(2_000_000_000n));
// Build the transfer instruction
const transferIx = getTransferSolInstruction({
source: client.payer,
destination: recipientAddress,
amount: lamports(1_000_000_000n), // 1 SOL
});
// Build the transaction message
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
tx => setTransactionMessageFeePayerSigner(client.payer, tx),
tx => setTransactionMessageLifetimeUsingBlockhash(
client.svm.latestBlockhashLifetime(),
tx
),
tx => appendTransactionMessageInstruction(transferIx, tx),
);
// Sign and send the transaction
const signedTx = await signTransactionMessageWithSigners(transactionMessage);
client.svm.sendTransaction(signedTx);
// Check new balances
const recipientBalance = client.svm.getBalance(recipientAddress) ?? 0n;
console.log('Recipient balance:', Number(recipientBalance) / 1e9, 'SOL');
console.log('Transfer successful!');Test complete! This test covers executing the transfer instruction from the system program.
Run All Tests
cargo test -- --show-outputYou should see:
running 1 test
test create_account ... ok
successes:
---- create_account stdout ----
Account funded with 1 SOL
successes:
create_account
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s
Running tests/test_transfer.rs (target/debug/deps/test_transfer-f174954e7483f36b)
running 1 test
test test_transfer ... ok
successes:
---- test_transfer stdout ----
Transfer successful!
successes:
test_transfer
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04snpx tsx basic-test.ts && npx tsx transfer-test.tsYou should see:
Balance: 5000000000n lamports
Recipient balance: 1 SOL
Transfer successful!That's it! Now you've successfully made litesvm tests that create and fund accounts and execute transactions.
Next Steps
Why LiteSVM → Learn why you would want to use LiteSVM for testing
- Testing Your Program → Learn how to test your own Solana program
- Examples → View copy-paste solutions for common scenarios and full repository examples
- API Reference → Learn advanced features and custom configurations
- Testing Your Program → Learn how to test your own Solana program using LiteSVM for TypeScript
- Examples → View copy-paste solutions for common scenarios
- API Reference → Learn advanced features and custom configurations