Test Client
A function to create a Test Client
A Test Client is an interface to "test" JSON-RPC API methods accessible through a local Ethereum test node such as Anvil or Hardhat such as mining blocks, impersonating accounts, setting fees, etc through Test Actions.
The createTestClient function sets up a Test RPC Client with a given Transport.
Import
import { } from 'viem'Usage
Initialize a Client with your desired Chain, Transport (e.g. http) and mode (e.g. "anvil").
import { , } from 'viem'
import { } from 'viem/chains'
const = ({
: ,
: 'anvil',
: (),
})Then you can consume Test Actions:
const = await .({ : 1 }) Extending with Public & Wallet Actions
When interacting with a Ethereum test node, you may also find yourself wanting to interact with Public Actions and Wallet Actions with the same chain and transport. Instead of creating three different Clients, you can instead just extend the Test Client with those actions:
import { , , , } from 'viem'
import { } from 'viem/chains'
const = ({
: ,
: 'anvil',
: (),
})
.()
.()
const = await .() // Public Action
const = await .({ ... }) // Wallet Action
const = await .({ : 1 }) // Test ActionParameters
mode
- Type:
"anvil" | "hardhat" | "ganache"
Mode of the Test Client.
const = ({
: ,
: 'anvil',
: (),
})transport
- Type: Transport
Transport of the Test Client.
const = ({
: ,
: 'anvil',
: (),
})account (optional)
- Type:
Account | Address
The Account to use for the Client. This will be used for Actions that require an account as an argument.
Accepts a JSON-RPC Account or Local Account (Private Key, etc).
import { } from 'viem/accounts'
const = ({
: ('0x...'),
: ,
: 'anvil',
: (),
})chain (optional)
- Type: Chain
Chain of the Test Client.
const = ({
: ,
: 'anvil',
: (),
})cacheTime (optional)
- Type:
number - Default:
client.pollingInterval
Time (in ms) that cached data will remain in memory.
const = ({
: 10_000,
: ,
: 'anvil',
: (),
})name (optional)
- Type:
string - Default:
"Test Client"
A name for the Client.
const = ({
: ,
: 'anvil',
: 'Anvil Client',
: (),
})pollingInterval (optional)
- Type:
number - Default:
4_000
Frequency (in ms) for polling enabled Actions.
const = ({
: ,
: 'anvil',
: 10_000,
: (),
})rpcSchema (optional)
- Type:
RpcSchema - Default:
TestRpcSchema
Typed JSON-RPC schema for the client.
import { } from 'viem'
type = [{
: 'eth_wagmi',
: [string]
: string
}]
const = ({
: ,
: <>(),
: ()
})
const = await .({
: 'eth_wa // [!code focus]
: ['hello'],
})