Anchor TS Tests
anchor test is the default integration path for Anchor 0.32.1 projects: compile programs, deploy to a local validator, and run TypeScript tests against the IDL. Use it for end-to-end flows with real RPC semantics.
Search across all documentation pages
anchor test is the default integration path for Anchor 0.32.1 projects: compile programs, deploy to a local validator, and run TypeScript tests against the IDL. Use it for end-to-end flows with real RPC semantics.
Quick-reference recipe card - copy-paste ready.
anchor test
# or with existing validator:
anchor test --skip-local-validatorimport * as anchor from "@coral-xyz/anchor";
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const program = anchor.workspace.MyProgram as Program<MyProgram>;When to reach for this:
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { MyProgram } from "../target/types/my_program";
import { Keypair, PublicKey, SystemProgram } from "@solana/web3.js";
import { TOKEN_PROGRAM_ID, createMint, getOrCreateAssociatedTokenAccount } from "@solana/spl-token";
import { assert } from "chai";
describe("my_program", () => {
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const program = anchor.workspace.MyProgram as Program<MyProgram>;
const payer = (provider.wallet as anchor.Wallet).payer;
it("initializes vault", async () => {
const user = Keypair.generate();
await provider.connection.confirmTransaction(
await provider.connection.requestAirdrop(user.publicKey, 2e9)
);
const mint = await createMint(provider.connection, payer, payer.publicKey, null, 6);
const ata = await getOrCreateAssociatedTokenAccount(
provider.connection, payer, mint, user.publicKey
);
const [vaultPda] = PublicKey.findProgramAddressSync(
[Buffer.from("vault"), user.publicKey.toBuffer()],
program.programId
);
await program.methods
.initVault()
.accounts({
user: user.publicKey,
vault: vaultPda,
mint: mint,
userToken: ata.address,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
})
.signers([user])
.rpc();
const vault = await program.account.vault.fetch(vaultPda);
assert.ok(vault.authority.equals(user.publicKey));
});
});What this demonstrates:
Anchor.toml..rpc() sends signed transaction and awaits confirmation.anchor test runs anchor build, starts validator (unless skipped), deploys programs, executes tests/**/*.ts.chai assertions common.target/idl and types to target/types.Anchor.toml pays deploy and test fees.| Field | Purpose |
|---|---|
[provider] cluster | localnet / devnet URL |
[scripts] test | Custom test command |
[test] startup_wait | Validator readiness delay |
// Match error codes from Rust #[error_code]
import { AnchorError } from "@coral-xyz/anchor";
try {
await program.methods.badIx().rpc();
} catch (e) {
const err = e as AnchorError;
console.log(err.error.errorCode.code);
}Anchor.toml devnet but validator localnet. Fix: Align provider URL with running cluster.anchor build before test.pkill solana-test-validator in CI pre-step.--skip-local-validator - Double validator start fails. Fix: Document CI validator lifecycle.| Alternative | Use When | Don't Use When |
|---|---|---|
| Bankrun | Faster TS tests | Need full RPC features |
| LiteSVM | Rust unit tests | TS-only team |
| Surfpool fork | Mainnet DeFi state | Simple CRUD program |
Mocha via ts-mocha in Anchor template - can switch to node:test with custom script.
Set provider cluster to devnet and run anchor test --skip-local-validator with funded wallet.
Workspace Cargo.toml members all deploy in anchor test when configured in Anchor.toml.
before hooks create mints/users; commit JSON keypairs only for localnet, never mainnet keys.
Add compute budget instructions in transaction builder when ix approaches CU limit.
Supported via web3.js v2 patterns - ensure client constructs v0 tx when testing ALTs.
cargo test runs Rust tests only; anchor test includes deploy + TS integration.
Usually validator state leakage - use --reset or unique keypairs per test.
Cache target/ and npm modules; still rebuild programs each run for correctness.
Install matching solana CLI in CI so local validator matches production runtime.
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.
Reviewed by Chris St. John·Last updated Jul 16, 2026