Diagnostics
Configured with AnvilContainer.withDiagnostics(), used through
StartedAnvilContainer.diagnostics (an AnvilDiagnostics). How it works:
Diagnostics concepts.
DiagnosticsOptions
| Method | Default | Notes |
|---|---|---|
disable() / enable() | on | off: nothing is recorded or written |
withOutputDir(dir) | anvil-diagnostics | folder of artifacts and reports, relative to the working directory (DEFAULT_DIAGNOSTICS_DIR) |
withVerbosity(level) | "standard" | "minimal", "standard" or "debug" (DiagnosticsVerbosity) — what artifacts contain |
withArtifacts(policy) | "on-failure" | "on-failure", "always" or "never" (DiagnosticsArtifactsPolicy) |
withLogLines(lines | "all") | 100 / 1 000 / all | node log lines in artifacts, by verbosity |
withGasProfiling(settings | false) | on | GasProfilingSettings: opcodes ("auto": with --steps-tracing; true requires it), maxTracedTransactions (20), minOpcodeShare (0.05) |
withRpcProfiling(settings | false) | on | RpcProfilingSettings: recordParams (debug only), maxRecordedCalls (500) |
withThresholds(thresholds, action?) | none, "warn" | DiagnosticsThresholds; merged with earlier calls; action "warn" or "fail" (DiagnosticsThresholdAction) |
withReports(...formats) | none | "json", "text" (DiagnosticsReportFormat): suite reports written by finishSuite() |
withConsole(output) | "failures" | "failures", "summary" (a line per test) or "silent" (DiagnosticsConsole) |
withGithubActions(mode) | "auto" | "auto" (when GITHUB_ACTIONS is set), "on", "off" (DiagnosticsGithubActions) |
withStateDump(enabled?) | debug only | dump the chain state into artifacts |
withAbis(...abis) | none | ABIs to name functions and decode custom errors |
clone() | – | independent copy |
build(configuration?, env?) | – | validated, frozen DiagnosticsConfiguration with environment overrides; with an AnvilConfiguration, opcodes: true requires --steps-tracing |
Invalid values throw AnvilConfigurationError naming the setting.
DiagnosticsThresholds: gasPerTest, gasPerTransaction,
rpcCallsPerTest, duplicateRpcCallsPerTest, rpcCallsPerMethod
({ method: limit }), gasPerSuite, rpcCallsPerSuite.
Environment variables
Read when the container starts; they override the code (DIAGNOSTICS_ENV).
| Variable | Values |
|---|---|
ANVIL_DIAGNOSTICS | off / false / 0 disables, on enables |
ANVIL_DIAGNOSTICS_DIR | output folder |
ANVIL_DIAGNOSTICS_VERBOSITY | minimal, standard, debug |
ANVIL_DIAGNOSTICS_ARTIFACTS | on-failure, always, never |
ANVIL_DIAGNOSTICS_RUN_ID | correlation id of the run (default: the GitHub run id and attempt) |
AnvilDiagnostics
| Member | Description |
|---|---|
enabled | false when turned off |
configuration | the DiagnosticsConfiguration in effect |
suite | suite name: the test file relative to the working directory (names its folder) |
reports | AnvilTestReport[] of the finished tests |
beginSuite(file?) | names the suite (detected when omitted) and empties its folder of earlier runs; useAnvil() calls it |
beginTest(name?) | starts recording a test in a fresh scope; useAnvil() calls it |
endTest(outcome?) | profiles the test, checks thresholds, writes artifacts when it failed; outcome detected from Jest, or pass TestOutcome, Vitest's hook ctx or Mocha's this (TestFrameworkContext); resolves to the AnvilTestReport |
runTest(name, fn) | runs fn as an isolated test (also when concurrent); artifacts when it throws, then rethrows |
measure(name, fn) | { result, profile }: the AnvilProfile of one operation; also counts for the enclosing test |
currentProfile() | AnvilProfile of the running test (or scope) so far |
reset() | forgets what the running test (or scope) recorded so far |
captureArtifacts(outcome?) | writes artifacts for the running test (or scope) now; resolves to its report |
registerAbi(...abis) | more ABIs for names and reverts (contractAbi() registers automatically) |
suiteReport() | AnvilSuiteReport so far |
exportReports(formats?, dir?) | writes suite-report.json / .txt; resolves to the paths |
finishSuite() | exports, GitHub job summary, suite thresholds; useAnvil() calls it in afterAll |
Only THRESHOLD_EXCEEDED (Errors) escapes endTest(),
runTest() and finishSuite(); other problems are reported on stderr.
Reports
Plain JSON data: gas as numbers, wei as decimal strings. formatReport(report)
renders an AnvilProfile, AnvilTestReport or AnvilSuiteReport as text.
| Type | Fields |
|---|---|
AnvilProfile | name, durationMs, gas, rpc |
AnvilTestReport | AnvilProfile + schemaVersion, test (TestMetadata), run (RunMetadata), node (NodeMetadata), warnings, hints, artifactsDir |
AnvilSuiteReport | suite, run, node, tests (counts), gas and rpc aggregates, outsideTests (AnvilProfile), testReports (SuiteTestSummary[]), warnings |
GasProfile | total, totalFee, succeeded, reverted, pending, rejected, transactions (TransactionGas[]), opcodes (OpcodeGas[]), opcodeProfiling (OpcodeProfilingStatus: "collected", "off", "unavailable", "none") |
TransactionGas | hash, status (success, reverted, pending, rejected), method, from, to, contractAddress, function, gasUsed, gasLimit, effectiveGasPrice, fee, blockNumber, revert (RevertInfo), error, opcodes (OpcodeGasProfile) |
OpcodeGasProfile / OpcodeGas | executionGas, intrinsicGas, steps, opcodes / op, count, gas, share |
RevertInfo | reason, errorName, args, data |
RpcProfile | total, errors, totalMs, methods (RpcMethodStats[]), duplicates (DuplicateRpcCall[]), duplicateCalls, failures (RpcFailure[]), calls (RpcCallRecord[]) |
DiagnosticsWarning | threshold, limit, actual, message |
TestMetadata | id, name, file, status (TestStatus: passed, failed, unknown), attempt, error, startedAt, endedAt, durationMs |
RunMetadata | id, environment, url, commit, ref, worker, pid, node |
NodeMetadata | rpcUrl, chainId, containerId, command, clientVersion, blockNumber, forking, stepsTracing |
TestOutcome | name, file, status, error, attempt for endTest() / captureArtifacts() |
Constants: DEFAULT_DIAGNOSTICS_DIR, DIAGNOSTICS_SCHEMA_VERSION,
EXPENSIVE_OPCODES (always listed when they ran), PANIC_CODES (meaning of
Panic(uint256) codes), DIAGNOSTICS_ENV (Exports).
Tasks: Diagnose failing tests, Profile gas usage, Profile JSON-RPC calls, Set budgets, Configure diagnostics.