Diagnostics: artifacts and profiling
Every started node has anvil.diagnostics: it records what the node's
clients do, profiles each test's gas and JSON-RPC volume, and turns a failure
into a folder of evidence with a summary of what to check.
How calls are recorded
All clients of a node — testClient, testClientFor(), createTestClient(),
rpc() and cheats — send through one transport wrapper. It times every
call and hands it to the diagnostics, which keep per method: count, errors,
time and identical repeats; per send: the hash (or the node's refusal), for
the gas profile.
| Counted | Not counted |
|---|---|
every call a node's clients send, once per call: batching (withBatch()) and viem's retries happen below the wrapper | eth_chainId: the clients answer it locally |
| failed calls, with their revert data decoded | the diagnostics' own reads: they use a separate channel |
| reverted and refused transactions | your own viem clients built from anvil.rpcUrl / anvil.chain |
WebSocket subscription notifications (eth_subscribe pushes) |
Scopes and isolation
A call is recorded in exactly one scope, the first of:
- the innermost
runTest()ormeasure()scope of the calling code — tracked with Node'sAsyncLocalStorage, so it survivesawaitandPromise.all; - the current test, between
beginTest()andendTest()(useAnvil()opens the next test right after restoring the previous one); - "outside tests": setup, hooks and restores (
outsideTestsof the suite report).
A measure() scope also reports to the scope it runs in; a runTest() scope
is a test of its own. Concurrent tests (test.concurrent, Promise.all)
therefore never see each other's calls, and parallel test files run in
separate Jest workers with their own nodes. reset() empties the running
scope, e.g. after fixtures built in beforeEach.
When a test fails
useAnvil()'s afterEach reads the test's outcome, profiles it and, when it
failed, captures the artifacts — then restores the baseline: the restore
(evm_revert) erases the test's blocks and empties the pool, the very
evidence a failure needs. A failing setup is captured as
useAnvil setup before the error is rethrown.
| Framework | Outcome read from |
|---|---|
| Jest | the running test's errors (jest-circus state) |
| Vitest | the hook's context: ctx.task.result |
| Mocha | this.currentTest.state |
| anything else | unknown: pass { status, error } to endTest(), or withArtifacts("always") |
Diagnostics never fail a test by accident: a problem while collecting is
reported on stderr and the test result stands. Only an exceeded budget with
withThresholds(…, "fail") fails a test (or suite), with
THRESHOLD_EXCEEDED.
What it costs
| Situation | Extra JSON-RPC round trips |
|---|---|
| passing test, no reverted transaction | none: receipts are cached from confirm() |
| receipts the test never read | one batch |
| reverted transactions | one batch of call traces (for the revert reason) |
opcode gas (--steps-tracing) | one batch of opcode traces, up to maxTracedTransactions |
| failing test | one batch for the pool and node info, the node's log, call traces |
Opcode gas
With --steps-tracing, debug_traceTransaction returns every EVM step with
the gas left before it and its gasCost. For CALL, CREATE and friends
gasCost includes the gas forwarded to the callee, so adding up
gasCost counts the callee twice. The diagnostics charge a call step only
what it costs itself — the gas left before it, minus the gas left after the
callee returned, minus what the callee's own steps used — so a CALL to a
cold account shows 2 600, not the 83 179 it forwarded, and the opcodes add up:
executionGas + intrinsicGas === gasUsed, where intrinsicGas is
21 000 plus calldata, minus refunds.
Costs follow the EVM's rules: after EIP-2929 the first access to a slot pays
the cold surcharge, so in balance += x the SLOAD costs 2 100 and the
SSTORE 20 000 (zero to non-zero) rather than 22 100.
Artifacts
anvil-diagnostics/ withOutputDir(), ANVIL_DIAGNOSTICS_DIR
└── test__e2e__vault.e2e.test.ts/ the suite: its test file
├── suite-report.json / .txt withReports()
└── vault-rejects-early-withdrawals-3f2a1c/ the test (hash of its full name)
├── summary.txt report.json anvil.log txpool.json
├── receipts.json node.json state.json (debug)
└── traces/<hash>.call.json, <hash>.opcodes.json
Names are stable across runs (retries get -attempt-2, …), and a suite's
folder is emptied when it starts, so it always shows the last run.
report.json carries schemaVersion (DIAGNOSTICS_SCHEMA_VERSION).
To correlate artifacts, every report names its test (test.id, file,
attempt), its run (run.id: the GitHub run id and attempt,
ANVIL_DIAGNOSTICS_RUN_ID, or a local id; worker, pid, commit, url)
and its node (rpcUrl, containerId, command, clientVersion, block
number).
Remediation hints
summary.txt, report.json and the console list what to check, most
specific first: each reverted transaction with its decoded error (custom
errors from the registered ABIs, Error(string), Panic codes explained),
out-of-gas, refused sends (nonce, funds, signer, fees), reverting reads and
estimates, transactions still pending, the meaning of a library error code,
exceeded budgets with their top consumers, and what would give more detail
(--steps-tracing, ABIs, logs).
CI
On GitHub Actions (GITHUB_ACTIONS, or withGithubActions("on")), a failing
test becomes an error annotation on its file with the artifacts' path and the
first hints, an exceeded budget a warning, and each suite adds a table to the
job summary. Upload anvil-diagnostics/ when a job fails
(Run in CI); raise the verbosity with
ANVIL_DIAGNOSTICS_VERBOSITY=debug when re-running a failed job.
Related: Diagnose failing tests, Profile gas usage, Isolation and snapshots.