Skip to main content

Set gas and JSON-RPC budgets

Set budgets with withThresholds(); exceeding one adds a warning to the report, the console and GitHub, and with "fail" fails the test:

const [from, to] = await node.addresses();
await node.sendEthTransaction(from, to, "1");
await node.sendEthTransaction(from, to, "1"); // 42 000 > gasPerTest 30 000

// In afterEach, endTest() throws this and fails the test:
await expect(
node.diagnostics.endTest({ status: "passed" }),
).rejects.toThrow(/Budget exceeded: .*gas 42,000 > gasPerTest 30,000/);
const report = node.diagnostics.reports.at(-1)!;
expect(report.warnings.map((warning) => warning.threshold)).toEqual([
"gasPerTest",
]);

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

ThresholdLimits
gasPerTestgas of all transactions of a test
gasPerTransactiongas of any one transaction
rpcCallsPerTestJSON-RPC calls of a test
duplicateRpcCallsPerTestrepeated identical calls of a test
rpcCallsPerMethodcalls per method and test, e.g. { eth_call: 100 }
gasPerSuite / rpcCallsPerSuitetotals of a test file, checked when it finishes

Suite reports​

const suite = await anvil.diagnostics.suiteReport();
expect(suite.suite).toBe("test/e2e/diagnostics-and-profiling.e2e.test.ts");
expect(suite.tests.total).toBe(anvil.diagnostics.reports.length);
expect(suite.gas.total).toBeGreaterThan(0);
expect(suite.rpc.methods[0].count).toBeGreaterThan(0); // busiest method
expect(suite.outsideTests.gas.transactions).toHaveLength(4); // setup
expect(formatReport(suite)).toContain("Anvil diagnostics · suite");

// finishSuite() (useAnvil's afterAll) writes the configured exports;
// exportReports() writes them on demand.
const [json, text] = await anvil.diagnostics.exportReports([
"json",
"text",
]);
expect(path.basename(json)).toBe("suite-report.json");
expect(fs.readFileSync(text, "utf8")).toContain("JSON-RPC by method");

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

With withReports("json", "text"), useAnvil() writes suite-report.json and suite-report.txt into the suite's folder when the file finishes; on GitHub Actions the job summary gets a table per suite.

Concepts​