LiteSVM
Write tests for Solana programs in Rust, TS/JS or Python using LiteSVM.
Overview
litesvm
is a fast and lightweight library for testing Solana programs.
It works by creating an in-process Solana VM optimized for program developers.
This makes it much faster to run and compile than alternatives like solana-program-test
and solana-test-validator
.
litesvm
is available in Rust, TS/JS and Python (as part of the solders
library).
Installation
Minimal Example
Deploying Programs
Most of the time we want to do more than just mess around with token transfers - we want to test our own programs.
Tip: if you want to pull a Solana program from mainnet or devnet, use the solana program dump
command from the Solana CLI.
To add a compiled program to our tests we can use .add_program_from_file
.
Here's an example using a simple program from the Solana Program Library that just does some logging:
Time travel
Many programs rely on the Clock
sysvar: for example, a mint that doesn't become available until after
a certain time. With litesvm
you can dynamically overwrite the Clock
sysvar
using svm.set_sysvar::<Clock>()
(or .setClock
in TS, or .set_clock
in Python).
Here's an example using a program that panics if clock.unix_timestamp
is greater than 100
(which is on January 1st 1970):
See also: warp_to_slot
, which lets you jump to a future slot.
Writing arbitrary accounts
LiteSVM lets you write any account data you want, regardless of whether the account state would even be possible.
Here's an example where we give an account a bunch of USDC, even though we don't have the USDC mint keypair. This is convenient for testing because it means we don't have to work with fake USDC in our tests:
Copying Accounts from a live environment
If you want to copy accounts from mainnet or devnet, you can use the solana account
command in the Solana CLI to save account data to a file.
Other features
Other things you can do with litesvm
include:
- Changing the max compute units and other compute budget behaviour using
.with_compute_budget
. - Disable transaction signature checking using
.with_sigverify(false)
. - Find previous transactions using
.get_transaction
.
When should I use solana-test-validator
?
While litesvm
is faster and more convenient, it is also less like a real RPC node.
So solana-test-validator
is still useful when you need to call RPC methods that LiteSVM
doesn't support, or when you want to test something that depends on real-life validator behaviour
rather than just testing your program and client code.
In general though it is recommended to use litesvm
wherever possible, as it will make your life
much easier.