JavaScript Testing Framework for Solana
Info
This is a beta version of the Solana Toolkit, and is still a WIP. Please post all feedback as a GitHub issue here.
Add Dependency #
Navigate to your smart contract directory and run:
npm install solana-bankrun
Bankrun Overview #
Bankrun is a fast and lightweight framework for testing Solana programs in NodeJS.
It uses the
solana-program-test
crate
under the hood and allows you to do things that are not possible with
solana-test-validator
, such as jumping back and forth in time or dynamically
setting account data.
Bankrun works by spinning up a lightweight BanksServer
that's like an RPC node
but much faster, and creating a BanksClient
to talk to the server. This runs
the Solana
Banks.
Minimal Example #
import { start } from "solana-bankrun";
import { PublicKey, Transaction, SystemProgram } from "@solana/web3.js";
test("one transfer", async () => {
const context = await start([], []);
const client = context.banksClient;
const payer = context.payer;
const receiver = PublicKey.unique();
const blockhash = context.lastBlockhash;
const transferLamports = 1_000_000n;
const ixs = [
SystemProgram.transfer({
fromPubkey: payer.publicKey,
toPubkey: receiver,
lamports: transferLamports,
}),
];
const tx = new Transaction();
tx.recentBlockhash = blockhash;
tx.add(...ixs);
tx.sign(payer);
await client.processTransaction(tx);
const balanceAfter = await client.getBalance(receiver);
expect(balanceAfter).toEqual(transferLamports);
});