Don’t rely on Testnet for integration tests. It’s slow, you need a faucet, and other people can mess up your state. near-kit includes a built-in Sandbox manager that runs a local NEAR node for you.
Setup
We recommend using a test runner like bun:test, jest, or vitest.
Pre-Funded Accounts
The sandbox comes with one “Root Account” (test.near) that has a massive balance. Use this account to create sub-accounts for your tests.
Patching State
Use patchState() to directly modify blockchain state without sending transactions. This is useful for setting up test scenarios quickly — for example, giving an account a specific balance or adding an access key.
Each StateRecord can be one of:
Fast-Forwarding Blocks
Use fastForward() to advance the blockchain by producing empty blocks. This is useful for testing time-dependent contract logic (e.g., lockups, vesting schedules) without waiting for real blocks.
State Snapshots
Take a snapshot of the entire blockchain state with dumpState(), then restore it later with restoreState(). This is great for running multiple tests against the same initial state.
Saving Snapshots to Disk
For expensive setup that you don’t want to repeat across test runs, save snapshots to disk:
Restarting the Sandbox
Use restart() to stop the sandbox process, clear all data, and start fresh. Block height resets to 0. Optionally pass a snapshot to bake into the genesis state — accounts from the snapshot will exist from block 0.
restart() with a snapshot is the most reliable way to reset state. Unlike restoreState() which patches on top of existing state, restart() merges snapshot records into the genesis file so the sandbox boots with exactly the state you want.
Custom Binary
By default, Sandbox.start() downloads the near-sandbox binary from NEAR’s servers. You can use a local binary instead:
Priority order:
binaryPath option (if provided)
NEAR_SANDBOX_BIN_PATH environment variable
- Download from NEAR’s S3 bucket
This is useful for:
- CI environments with pre-cached binaries
- Testing against a custom-built sandbox
- Offline development