Diagnose failing tests
With useAnvil(), every failing test leaves its evidence in
anvil-diagnostics/<test file>/<test>/, captured before the state is
restored, and says what to check on the console:
[anvil-diagnostics] ✖ vault rejects deposits without approval — artifacts: anvil-diagnostics/test__vault.test.ts/vault-rejects-deposits-without-approval-3f2a1c/summary.txt
1. deposit(uint256) 0xfc82a321… on 0xe7f1725e7734ce288f8367e1bb143e90bb3f0512 reverted: InsufficientAllowance(0xe7f1…0512, 0, 1).
2. Check the preconditions behind InsufficientAllowance (allowances, balances, roles, time locks) in the steps before this transaction.
3. The failing call frame is in traces/0xfc82a321….call.json (look for "error" and "output").
gas 73,120 in 2 tx (1 reverted, 0 rejected) · 6 JSON-RPC calls (0 failed)
Diagnostics are on by default; tune them on the container:
const anvil = useAnvil(
new AnvilContainer(
new AnvilOptions().evm.withStepsTracing(), // opcode-level gas
)
.withTestClient((client) => client.withAccount(0))
.withDiagnostics(
(diagnostics) =>
diagnostics
.withVerbosity("standard") // "minimal" | "standard" | "debug"
.withAbis(exampleToken.abi, timeLockVault.abi) // decode reverts, name functions
.withThresholds({ gasPerTest: 1_000_000, rpcCallsPerTest: 200 }) // warn above
.withReports("json", "text"), // suite report when the file finishes
),
{
setup: async (node) => {
[, alice, bob] = await node.addresses();
token = await deployToken(node.testClient);
vault = await deployVault(node.testClient, token, 0n);
await mint(node.testClient, token, alice, parseEther("10"));
await mint(node.testClient, token, bob, parseEther("10"));
},
},
);
Tested in examples/test/e2e/diagnostics-and-profiling.e2e.test.ts:34
What a failing test leaves behind
// useAnvil() does this for every failing test; runTest() runs one inline.
await expect(
anvil.diagnostics.runTest("bob deposits without approval", async () => {
const hash = await anvil.testClientFor(bob).writeContract({
address: vault,
abi: timeLockVault.abi,
functionName: "deposit",
args: [1n],
});
await anvil.confirm(hash); // throws: reverted
}),
).rejects.toBeInstanceOf(AnvilTransactionRevertedError);
const report = anvil.diagnostics.reports.at(-1)!;
expect(report.test.status).toBe("failed");
expect(report.hints[0]).toMatch(
/^deposit\(uint256\) 0x[0-9a-f]{8}… on 0x[0-9a-fA-F]{40} reverted: InsufficientAllowance\(/,
);
const files = fs.readdirSync(report.artifactsDir!);
expect(files).toEqual(
expect.arrayContaining([
"summary.txt", // start here: what failed, hints, gas, JSON-RPC
"report.json", // everything, machine-readable
"anvil.log", // the node's log
"txpool.json", // pending and queued transactions
"receipts.json",
"traces", // call trace per transaction
"node.json", // command line, anvil_nodeInfo, anvil_metadata
]),
);
const summary = fs.readFileSync(
path.join(report.artifactsDir!, "summary.txt"),
"utf8",
);
expect(summary).toContain("✖ FAILED · bob deposits without approval");
expect(summary).toContain("What to check:");
Tested in examples/test/e2e/diagnostics-and-profiling.e2e.test.ts:172
| File | Contents |
|---|---|
summary.txt | start here: the error, what to check, gas and JSON-RPC tables, the node's log lines about the test's transactions |
report.json | the full report: test, run and node metadata, every transaction, the JSON-RPC call log, hints |
anvil.log | the node's log (last 100 / 1 000 lines, or all at debug) |
txpool.json | pending and queued transactions when the test ended |
traces/<hash>.call.json | call trace (callTracer, with logs) per transaction |
traces/<hash>.opcodes.json | opcode steps of reverted transactions (debug, needs --steps-tracing) |
receipts.json, node.json | receipts; command line, anvil_nodeInfo, anvil_metadata |
state.json | the chain state (debug): anvil --load-state state.json to inspect it |
In CI, upload the folder when a job fails (Run in CI).
Keep the evidence of a handled failure
anvil.diagnostics.registerAbi(priceConsumer.abi); // e.g. a contract deployed later
try {
await anvil.confirm(
anvil.testClientFor(bob).writeContract({
address: vault,
abi: timeLockVault.abi,
functionName: "withdraw",
}),
);
} catch (error) {
// Keep the evidence before cleaning up, then carry on.
const report = await anvil.diagnostics.captureArtifacts({
name: "withdraw before depositing",
error,
});
expect(report.gas.transactions[0].revert?.errorName).toBe(
"NothingToWithdraw",
);
expect(fs.existsSync(report.artifactsDir!)).toBe(true);
}
Tested in examples/test/e2e/diagnostics-and-profiling.e2e.test.ts:254
Without useAnvil()
beforeAll(async () => {
node = await new AnvilContainer()
.withDiagnostics((diagnostics) =>
diagnostics.withThresholds({ gasPerTest: 30_000 }, "fail"),
)
.start();
node.diagnostics.beginSuite(); // names the suite, clears its old artifacts
}, 120_000);
beforeEach(() => node.diagnostics.beginTest());
// Profiles the test, checks budgets, writes artifacts when it failed.
afterEach(() => node.diagnostics.endTest());
afterAll(async () => {
await node?.diagnostics.finishSuite(); // exports and GitHub summary
await node?.stop();
});
Tested in examples/test/e2e/diagnostics-and-profiling.e2e.test.ts:317
endTest() reads the outcome from Jest, Vitest (pass the hook's ctx) and
Mocha (pass this).