Skip to main content
Skill — Copyable reference implementation. Use as-is or customize. See Skills Philosophy.

Viem Test Client

A copyable implementation of viem’s test client abstraction for local test nodes.

Overview

This Skill provides a complete viem-compatible test client including:
  • createTestClient - Factory returning test client with bound actions
  • mine - Mine blocks
  • setBalance - Set account balance
  • setCode - Set contract bytecode
  • setStorageAt - Set storage slot
  • setNonce - Set account nonce
  • impersonateAccount - Send transactions as any address
  • stopImpersonatingAccount - Stop impersonation
  • snapshot / revert - Save and restore EVM state
  • increaseTime / setNextBlockTimestamp - Time manipulation
  • reset - Reset fork state
  • dumpState / loadState - Serialize and restore state

Quick Start with Anvil

Snapshot and Revert

Save and restore EVM state:
Snapshots are consumed when reverted. Take a new snapshot after reverting if you need to revert again.

Account Impersonation

Send transactions from any address without the private key:

Time Manipulation

Control block timestamps for testing time-dependent contracts:

State Manipulation

Modify accounts directly:

Fork Management

Reset and manage forked state:

State Serialization

Save and restore entire state:

Automine Control

Control automatic block mining:

Mode Differences

Different test nodes use different RPC methods: The test client handles these differences automatically based on the mode parameter.

Hardhat Example

Ganache Example

Ganache does not support impersonateAccount, setNonce, or some other test actions. Use Anvil or Hardhat for full functionality.

Files

Copy these files into your codebase:
  • createTestClient.js - Main factory function
  • mine.js - Mine blocks
  • setBalance.js - Set account balance
  • setCode.js - Set contract code
  • setStorageAt.js - Set storage slot
  • setNonce.js - Set account nonce
  • impersonateAccount.js - Impersonate address
  • stopImpersonatingAccount.js - Stop impersonating
  • snapshot.js - Create EVM snapshot
  • revert.js - Revert to snapshot
  • increaseTime.js - Advance time
  • setNextBlockTimestamp.js - Set next block timestamp
  • dropTransaction.js - Remove tx from mempool
  • reset.js - Reset fork
  • dumpState.js - Serialize state
  • loadState.js - Load state
  • setAutomine.js - Control automine
  • TestClientTypes.ts - TypeScript type definitions
  • errors.ts - Custom error classes
  • index.ts - Module exports

Differences from Viem

This implementation:
  1. Uses Voltaire’s Address and Hex primitives
  2. Requires explicit provider (no transport abstraction)
  3. Does not include chain configuration
  4. Simplified error handling
For production use, consider adding:
  • Transport abstraction (HTTP, WebSocket, IPC)
  • Chain configuration with native currency
  • Request batching
  • Retry logic for RPC calls