Skip to main content

Diagnostics

Configured with AnvilContainer.withDiagnostics(), used through StartedAnvilContainer.diagnostics (an AnvilDiagnostics). How it works: Diagnostics concepts.

DiagnosticsOptions​

MethodDefaultNotes
disable() / enable()onoff: nothing is recorded or written
withOutputDir(dir)anvil-diagnosticsfolder 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 / allnode log lines in artifacts, by verbosity
withGasProfiling(settings | false)onGasProfilingSettings: opcodes ("auto": with --steps-tracing; true requires it), maxTracedTransactions (20), minOpcodeShare (0.05)
withRpcProfiling(settings | false)onRpcProfilingSettings: 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 onlydump the chain state into artifacts
withAbis(...abis)noneABIs 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).

VariableValues
ANVIL_DIAGNOSTICSoff / false / 0 disables, on enables
ANVIL_DIAGNOSTICS_DIRoutput folder
ANVIL_DIAGNOSTICS_VERBOSITYminimal, standard, debug
ANVIL_DIAGNOSTICS_ARTIFACTSon-failure, always, never
ANVIL_DIAGNOSTICS_RUN_IDcorrelation id of the run (default: the GitHub run id and attempt)

AnvilDiagnostics​

MemberDescription
enabledfalse when turned off
configurationthe DiagnosticsConfiguration in effect
suitesuite name: the test file relative to the working directory (names its folder)
reportsAnvilTestReport[] 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.

TypeFields
AnvilProfilename, durationMs, gas, rpc
AnvilTestReportAnvilProfile + schemaVersion, test (TestMetadata), run (RunMetadata), node (NodeMetadata), warnings, hints, artifactsDir
AnvilSuiteReportsuite, run, node, tests (counts), gas and rpc aggregates, outsideTests (AnvilProfile), testReports (SuiteTestSummary[]), warnings
GasProfiletotal, totalFee, succeeded, reverted, pending, rejected, transactions (TransactionGas[]), opcodes (OpcodeGas[]), opcodeProfiling (OpcodeProfilingStatus: "collected", "off", "unavailable", "none")
TransactionGashash, status (success, reverted, pending, rejected), method, from, to, contractAddress, function, gasUsed, gasLimit, effectiveGasPrice, fee, blockNumber, revert (RevertInfo), error, opcodes (OpcodeGasProfile)
OpcodeGasProfile / OpcodeGasexecutionGas, intrinsicGas, steps, opcodes / op, count, gas, share
RevertInforeason, errorName, args, data
RpcProfiletotal, errors, totalMs, methods (RpcMethodStats[]), duplicates (DuplicateRpcCall[]), duplicateCalls, failures (RpcFailure[]), calls (RpcCallRecord[])
DiagnosticsWarningthreshold, limit, actual, message
TestMetadataid, name, file, status (TestStatus: passed, failed, unknown), attempt, error, startedAt, endedAt, durationMs
RunMetadataid, environment, url, commit, ref, worker, pid, node
NodeMetadatarpcUrl, chainId, containerId, command, clientVersion, blockNumber, forking, stepsTracing
TestOutcomename, 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.