Skip to main content

Configure diagnostics

Diagnostics are on by default; every setting is validated as you set it:

const diagnostics = new DiagnosticsOptions()
.withOutputDir("test-results/anvil") // default: anvil-diagnostics/
.withVerbosity("debug") // + opcode traces, call params, full log, state dump
.withArtifacts("on-failure") // or "always" / "never"
.withLogLines(5_000) // node log lines in artifacts (default by verbosity)
.withStateDump(false) // skip the (large) state dump of debug
.withGasProfiling({
opcodes: "auto", // when anvil runs with --steps-tracing
maxTracedTransactions: 10, // per test
minOpcodeShare: 0.1, // list opcodes above 10 % of the execution gas
})
.withRpcProfiling({ recordParams: true, maxRecordedCalls: 1_000 })
.withThresholds(
{
gasPerTest: 2_000_000,
gasPerTransaction: 500_000,
rpcCallsPerTest: 300,
duplicateRpcCallsPerTest: 50,
rpcCallsPerMethod: { eth_call: 100 },
gasPerSuite: 50_000_000,
rpcCallsPerSuite: 5_000,
},
"warn", // or "fail": THRESHOLD_EXCEEDED fails the test / suite
)
.withReports("json", "text")
.withConsole("summary") // a line per test; "failures" (default), "silent"
.withGithubActions("auto") // annotations + job summary when on GitHub
.withAbis(exampleToken.abi);

const container = new AnvilContainer().withDiagnostics(diagnostics);
expect(container.diagnosticsOptions).toBe(diagnostics);
expect(diagnostics.build(undefined, {})).toMatchObject({
outputDir: path.resolve("test-results/anvil"),
verbosity: "debug",
logLines: 5_000,
stateDump: false,
gas: { opcodes: "auto", maxTracedTransactions: 10 },
thresholds: { gasPerTest: 2_000_000 },
});

Tested in examples/test/unit/diagnostics.test.ts:18

VerbosityFailure artifacts
minimalsummary, report, last 100 log lines, pool status, call traces of reverted transactions
standard (default)+ last 1 000 log lines, pool content, call traces of every transaction
debug+ full log, txpool_inspect, opcode steps of reverted transactions, call params, state dump

Gas and JSON-RPC profiles are in every report, whatever the verbosity.

From CI, without code changes​

const env = {
[DIAGNOSTICS_ENV.verbosity]: "debug", // ANVIL_DIAGNOSTICS_VERBOSITY
[DIAGNOSTICS_ENV.outputDir]: "/tmp/anvil-diagnostics", // ANVIL_DIAGNOSTICS_DIR
[DIAGNOSTICS_ENV.artifacts]: "always", // ANVIL_DIAGNOSTICS_ARTIFACTS
GITHUB_ACTIONS: "true", // set by GitHub: annotations and job summary
};
const configuration = new DiagnosticsOptions()
.withVerbosity("minimal")
.build(undefined, env); // start() reads process.env
expect(configuration).toMatchObject({
verbosity: "debug",
outputDir: "/tmp/anvil-diagnostics",
artifacts: "always",
githubActions: true,
});
expect(
new DiagnosticsOptions().build(undefined, { ANVIL_DIAGNOSTICS: "off" })
.enabled,
).toBe(false);

Tested in examples/test/unit/diagnostics.test.ts:62

Presets, and turning it off​

const ci = new DiagnosticsOptions().withVerbosity("debug");
const strict = ci.clone().withThresholds({ gasPerTest: 500_000 }, "fail");
expect(ci.build(undefined, {}).thresholds).toEqual({}); // untouched

const quiet = new AnvilContainer().withDiagnostics(false); // nothing recorded
expect(quiet.diagnosticsOptions.build(undefined, {}).enabled).toBe(false);
expect(strict.clone().disable().enable().build(undefined, {}).enabled).toBe(
true,
);

Tested in examples/test/unit/diagnostics.test.ts:86

Checked before start​

// Opcode gas needs anvil's --steps-tracing: checked by prepare() / start().
const container = new AnvilContainer().withDiagnostics((d) =>
d.withGasProfiling({ opcodes: true }),
);
expect(() => container.prepare()).toThrow(AnvilConfigurationError);
expect(() => container.prepare()).toThrow(/evm\.withStepsTracing\(\)/);
expect(() =>
new AnvilContainer(new AnvilOptions().evm.withStepsTracing())
.withDiagnostics((d) => d.withGasProfiling({ opcodes: true }))
.prepare(),
).not.toThrow();

// Values are validated as you set them:
expect(() =>
new DiagnosticsOptions().withThresholds({ gasPerTset: 1 } as never),
).toThrow(/unknown threshold "gasPerTset"/);

Tested in examples/test/unit/diagnostics.test.ts:100

In effect​

expect(anvil.diagnostics.enabled).toBe(true); // ANVIL_DIAGNOSTICS=off disables
expect(anvil.diagnostics.suite).toBe(
"test/e2e/diagnostics-and-profiling.e2e.test.ts", // names the artifacts folder
);
expect(anvil.diagnostics.configuration).toMatchObject({
verbosity: "standard",
artifacts: "on-failure",
reports: ["json", "text"],
});

Tested in examples/test/e2e/diagnostics-and-profiling.e2e.test.ts:300

Concepts​