This guide explains how to set up code coverage analysis for your Anchor program. 📊
Install llvm (preferably version 19+)
Ubuntu
sudo apt install llvm
MacOS
brew install llvm
cd programs/your_program
cargo add solana-coverage
#[cfg(not(target_os = "solana"))]
mod coverage {
use super::*;
use anchor_lang::solana_program::{
entrypoint::ProgramResult,
program_stubs::{set_syscall_stubs, SyscallStubs},
};
solana_coverage::anchor_coverage!();
}
#[program]
pub mod my_anchor_program {
...
}
3. [OPTIONAL] For third-party program binaries (if using CPI) create (if missing) and use: 📁 tests/fixtures
git clone https://github.com/LimeChain/anchor
cd anchor && cargo build
Don't clone litesvm inside the anchor-coverage-example clone directory. Please clone it outside of it as this may break tests.
git clone https://github.com/LimeChain/litesvm
cd litesvm/crates/node-litesvm && yarn && yarn build
Finally in order for the Typescript tests to use the enhanced version of LiteSVM we've just built, create a symbolic link to it at the root directory of the anchor workspace. Be sure to use full path:
ln -s /path/to/enhanced/litesvm/crates/node-litesvm/litesvm local-litesvm
Add your program and third-party programs to test coverage in your test file:
// Be sure to use the enhanced LiteSVM
import {
FailedTransactionMetadata,
LiteSVM,
TransactionMetadata,
} from "../local-litesvm";
// In your test file
describe("your_program", () => {
before(async () => {
// ... other setup code ...
// LiteSVM setup
svm.addProgramFromFile(programID, "target/deploy/your_program.so");
svm.addProgramFromFile(
thirdPartyProgramID,
"tests/fixtures/third_party.so"
); // Add third party programs (if any)
// Enable coverage tracking
svm.withCoverage(
[["your_program", programID.toBuffer()]],
[["third_party", thirdPartyProgramID.toBuffer()]],
payer.secretKey // add payer
);
});
// ... your tests ...
});
RUST_BACKTRACE=1 RUST_LOG=info path/to/enhanced/anchor/target/debug/anchor coverage
Supported environment variables that anchor-cli will interpret if user is willing to specify the path to specific tools used for code coverage generation:
LLVM_COV=llvm-cov-19 # default: llvm-cov
LLVM_PROFDATA=llvm-profdata-19 # default: llvm-profdata
HTML_VIEWER=/bin/true # default: open
The current repo showcases a simple vault program with code coverage enabled.
To test it create the symbolic link to the enhanced liteSVM (step 5) and generate the coverage with the enhanced Anchor CLI (step 4 and Coverage section).
Also be sure to have everything from the Prerequisites section installed.