Skip to main content

Overview

The anchor test command builds programs, starts a local validator, deploys programs, and runs your test suite.

Command Syntax

Alias

Options

Test Configuration

string
Build and test only this program
flag
default:"false"
Skip deploying programs (test against previously deployed programs)
flag
default:"false"
Skip building the program in the workspace
flag
default:"false"
Skip checking for safety comments (“CHECK”) in the code
flag
default:"false"
Do not build the IDL

Validator Options

flag
default:"false"
Skip starting a local validator (if configured cluster is localnet)
enum
default:"surfpool"
Validator type to use for local testingOptions:
  • surfpool: Use Surfpool validator (default, faster)
  • legacy: Use Solana test validator
flag
default:"false"
Keep the local validator running after tests complete

Build Options

enum
default:"sbf"
Architecture to use when building the programOptions: sbf, bpf
array
Environment variables to pass into the Docker container
string
Arguments to pass to the underlying cargo build-sbf command (use -- to separate)

Test Suite Options

array
Run test suites under the specified path(s)
string
Additional arguments to pass to the test runner

Examples

Basic Test

Run all tests with automatic build and deployment:
Output:

Test Specific Program

Skip Build (Faster Testing)

Skip building when the program code hasn’t changed:

Test Against Deployed Programs

Test without redeploying programs:

Keep Validator Running

Useful for inspecting transactions after tests:

Run Specific Test Files

Use Legacy Validator

Pass Arguments to Test Runner

Complete Skip (No Build, No Deploy)

Test Workflow

The test command executes the following steps:
  1. Build Phase (unless --skip-build):
    • Compiles Rust programs
    • Generates IDL files
    • Runs build hooks
  2. Validator Phase (unless --skip-local-validator):
    • Starts local validator (Surfpool or legacy)
    • Waits for validator to be ready
  3. Deploy Phase (unless --skip-deploy):
    • Deploys all programs to the local validator
    • Uploads IDL for each program
  4. Test Phase:
    • Runs the test script defined in Anchor.toml
    • Executes test suites (Mocha, Jest, etc.)
  5. Cleanup Phase:
    • Stops the validator (unless --detach)

Validator Types

Surfpool (Default)

  • Faster startup time
  • Optimized for Anchor development
  • Managed automatically by Anchor

Legacy

  • Standard solana-test-validator
  • Compatible with all Solana CLI features
  • Useful for advanced testing scenarios

Test Configuration

The test script is defined in Anchor.toml:
For Jest:

Environment Setup

Tests have access to:
  • anchor.workspace.<ProgramName>: Program clients
  • anchor.AnchorProvider.env(): Provider connected to local validator
  • anchor.web3: Solana web3.js library

Example Test (TypeScript)

Performance Tips

Use --skip-build when iterating on tests without changing program code to save time.
Use --skip-deploy when testing client-side logic against already deployed programs.
The Surfpool validator (default) is significantly faster than the legacy validator for most use cases.

Notes

The --detach flag keeps the validator running. Remember to stop it manually when done.
When testing on devnet or mainnet (not localnet), the validator is not started automatically.
The default timeout for Mocha tests is 1000000ms (1000 seconds) to accommodate Solana’s transaction confirmation times.