Skip to content

Inline configuration for Solidity tests

Hardhat lets you override fuzz and invariant test settings on a per-function basis using NatSpec comments. This is useful when different tests need different parameters. For example, running more fuzz iterations on a critical function, or increasing the depth of an invariant test.

Inline overrides take precedence over the global settings defined in your Solidity tests configuration.

Each override is a single line inside a NatSpec comment (line /// or block /** */), following the format hardhat-config: <key> = <value>:

/// hardhat-config: fuzz.runs = 10000
/// hardhat-config: fuzz.maxTestRejects = 500
function testTransferFuzz(uint256 amount) public {
// ...
}

Block comments are also supported:

/**
* hardhat-config: invariant.runs = 100
* hardhat-config: invariant.depth = 50
* hardhat-config: invariant.failOnRevert = true
*/
function invariantBalanceAlwaysPositive() public {
// ...
}
KeyDescription
fuzz.runsNumber of fuzz iterations to run
fuzz.maxTestRejectsMaximum number of rejected inputs before aborting
fuzz.showLogsWhether to show console logs during fuzzing
fuzz.timeoutTimeout for the fuzz test
invariant.runsNumber of invariant test runs
invariant.depthNumber of calls per run to attempt to break the invariant
invariant.failOnRevertWhether to fail the invariant if a revert occurs
invariant.callOverrideWhether to override unsafe external calls
invariant.timeoutTimeout for the invariant test
allowInternalExpectRevertAllow expecting reverts at the same callstack depth as the test

Hardhat also accepts the forge-config: prefix, kebab-case keys, and the default profile, so existing Foundry inline configuration works without changes:

/// forge-config: default.fuzz.runs = 10000
/// forge-config: fuzz.max-test-rejects = 500
function testTransferFuzz(uint256 amount) public {
// ...
}