Skip to main content

TestClientOptions

For AnvilContainer.withTestClient() and createTestClient().

const client = new TestClientOptions()
.withTransport("webSocket") // eth_subscribe-based watchers
.withTimeout(5_000) // per request (default 10 000 ms)
.withRetries(0, 50) // count, base delay in ms (default 3, 150)
.withCacheTime(1_000) // reuse e.g. the block number for 1 s (default 0)
.withPollingInterval(10) // watchers and receipt waits (default 25 ms)
.withLocalAccount(0); // dev account 0, signed locally

expect(client.build()).toMatchObject({
transport: "webSocket",
timeout: 5_000,
retryCount: 0,
retryDelay: 50,
account: { kind: "index", index: 0, signing: "local" },
});
// JSON-RPC batching needs HTTP:
expect(() => client.clone().withBatch().build()).toThrow(
AnvilTestClientConfigurationError,
);

Tested in examples/test/unit/configuration.test.ts:82

MethodDefaultNotes
withTransport(transport)"http""http" or "webSocket" (same port)
withPollingInterval(ms)25watchers, receipt waits
withCacheTime(ms)0viem's response cache (e.g. block number)
withTimeout(ms)10 000per JSON-RPC request
withRetries(count, delayMs?)3, 150exponential backoff
withBatch(batch?)offtrue, { batchSize, wait } or false; HTTP only
withAccount(index | address, { impersonate? })nonedefault account signed by anvil; impersonate: true unlocks a non-dev address at startup
withLocalAccount(index | privateKey | LocalAccount)nonedefault account that signs locally
withoutAccount()–removes the default account
withChain(overrides)Anvil (<id>), ETHname, nativeCurrency, contracts, blockExplorers, testnet (TestClientChainOverrides)
withMulticall3(address?, blockCreated?)–registers Multicall3 (MULTICALL3_ADDRESS by default)
clone()–independent copy
build(configuration?)–validated, frozen TestClientConfiguration; with an AnvilConfiguration, also checked against the flags

Setters validate at once; before start, settings are checked against the anvil options (AnvilTestClientConfigurationError):

RuleExample
A dev account index must existwithAccount(5) with account.withAccounts(2)
Local signing needs a readable keyrandom / seeded mnemonic + logs.quiet() hides the key banner
anvil must be able to sign for an addressnot a dev account, no { impersonate: true }, no autoImpersonate()
Batching needs HTTPwithBatch() with "webSocket"

Types: TestClientConfiguration, TestClientTransport, TestClientBatchOptions, TestClientChainOverrides, TestClientAccountSpec. Defaults: DEFAULT_POLLING_INTERVAL_MS, DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_RETRY_COUNT, DEFAULT_RETRY_DELAY_MS (Exports).

Task: Configure the test client.