| title | Testing Smart Contracts | ||
|---|---|---|---|
| tags |
|
||
| sidebar_position | 8 | ||
| description | Learn how to write and run tests for your Aztec smart contracts using Aztec.js and a local network. |
This guide covers how to test Aztec smart contracts by connecting to a local network, deploying contracts, and verifying their behavior.
- A running local Aztec network
- A compiled contract artifact (see How to compile a contract)
- Node.js test framework (Jest, Vitest, or similar)
Connect to your local Aztec network and create an embedded wallet:
#include_code connect_to_network /docs/examples/ts/aztecjs_connection/index.ts typescript
The EmbeddedWallet manages accounts, tracks deployed contracts, and handles transaction proving. It connects to the Aztec node which provides access to both the Private eXecution Environment (PXE) and the network.
The local network comes with pre-funded accounts. Load them into your wallet:
#include_code load_test_accounts /docs/examples/ts/aztecjs_testing/index.ts typescript
Deploy contracts using the generated contract class:
#include_code deploy_test_contract /docs/examples/ts/aztecjs_testing/index.ts typescript
Use .simulate() to read contract state without creating a transaction:
#include_code simulate_function /docs/examples/ts/aztecjs_connection/index.ts typescript
Simulations are free (no gas cost) and return the function's result directly. Use them for:
- Checking balances and state before/after transactions
- Validating expected outcomes in assertions
- Debugging contract behavior
Send transactions and wait for confirmation:
#include_code send_transaction /docs/examples/ts/aztecjs_connection/index.ts typescript
The send() method returns when the transaction is included in a block.
Here's a complete test example showing the typical structure with setup, test cases, and assertions:
#include_code complete_test_example /docs/examples/ts/aztecjs_testing/index.ts typescript
Test that invalid operations revert as expected:
#include_code test_revert_case /docs/examples/ts/aztecjs_testing/index.ts typescript
Use .simulate() to test reverts without spending gas. The simulation will throw if the transaction would fail onchain.
.simulate() accepts an overrides option that injects values into the simulator's (ephemeral) world-state fork and contract DB before the call runs. The override is scoped to that single simulation and thrown away afterwards.
Override a public-storage slot:
const result = await contract.methods.read_balance(account).simulate({
overrides: {
publicStorage: [{ contract: contract.address, slot: BALANCE_SLOT, value: new Fr(1_000_000n) }],
},
});Use this to set up state preconditions, reproduce production bugs against pinned storage, or exercise rare value branches without orchestrating the contract calls that produce them.