Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/content/docs/docs/guides/testing/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,6 @@ Hardhat 3 includes built-in support for code coverage and gas statistics. These
## Learn more

- To learn how to write tests in Solidity, read [this guide](/docs/guides/testing/using-solidity)
- To learn how to override test settings per-function, read the [inline configuration guide](/docs/guides/testing/inline-configuration)
- To learn how to write tests in TypeScript using [`node:test`](https://nodejs.org/api/test.html) and [viem](https://viem.sh), read [this guide](/docs/guides/testing/using-viem)
- To learn how to write tests in TypeScript using [Mocha](https://mochajs.org) runner and [ethers.js](https://github.com/ethers-io/ethers.js), read [this guide](/docs/guides/testing/using-ethers)
63 changes: 63 additions & 0 deletions src/content/docs/docs/guides/testing/inline-configuration.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: Inline configuration for Solidity tests
description: How to override fuzz and invariant test settings
Comment thread
schaable marked this conversation as resolved.
Outdated
Comment thread
schaable marked this conversation as resolved.
Outdated
sidebar:
label: Inline configuration
Comment thread
schaable marked this conversation as resolved.
Outdated
order: 3
Comment thread
schaable marked this conversation as resolved.
Outdated
---

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.
Comment thread
schaable marked this conversation as resolved.
Outdated

Inline overrides take precedence over the global settings defined in your [Solidity tests configuration](/docs/reference/configuration#solidity-tests-configuration).

## Syntax

Each override is a single line inside a NatSpec comment block, following the format `hardhat-config: <key> = <value>`:
Comment thread
schaable marked this conversation as resolved.
Outdated

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

Block comments are also supported:

```solidity
/**
* hardhat-config: invariant.runs = 100
* hardhat-config: invariant.depth = 50
* hardhat-config: invariant.failOnRevert = true
*/
function invariantBalanceAlwaysPositive() public {
// ...
}
```

## Supported configuration keys
Comment thread
schaable marked this conversation as resolved.

| Key | Description |
| --------------------------- | --------------------------------------------------------------- |
| `fuzz.runs` | Number of fuzz iterations to run |
| `fuzz.maxTestRejects` | Maximum number of rejected inputs before aborting |
| `fuzz.showLogs` | Whether to show console logs during fuzzing |
| `fuzz.timeout` | Timeout for the fuzz test |
| `invariant.runs` | Number of invariant test runs |
| `invariant.depth` | Number of calls per run to attempt to break the invariant |
| `invariant.failOnRevert` | Whether to fail the invariant if a revert occurs |
| `invariant.callOverride` | Whether to override unsafe external calls |
| `invariant.timeout` | Timeout for the invariant test |
Comment thread
schaable marked this conversation as resolved.
Outdated
| `allowInternalExpectRevert` | Allow expecting reverts at the same callstack depth as the test |

Comment thread
schaable marked this conversation as resolved.
Outdated
## Foundry compatibility

Hardhat also accepts the `forge-config:` prefix, kebab-case keys, and the `default` profile, so existing Foundry inline configuration works without changes:
Comment thread
schaable marked this conversation as resolved.
Outdated

```solidity
/// forge-config: default.fuzz.runs = 10000
/// forge-config: fuzz.max-test-rejects = 500
Comment thread
schaable marked this conversation as resolved.
function testTransferFuzz(uint256 amount) public {
// ...
}
```
2 changes: 1 addition & 1 deletion src/content/docs/docs/guides/testing/using-ethers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Testing your smart contracts with Ethers and Mocha
description: Learn how to test your smart contracts using Ethers.js and Mocha.
sidebar:
label: Using TypeScript & ethers.js
order: 4
order: 5
---

import { Steps } from "@astrojs/starlight/components";
Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/docs/guides/testing/using-solidity.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,5 @@ export default defineConfig({
```

To learn more about how to configure your Solidity tests, read [the Solidity tests Configuration reference](/docs/reference/configuration#solidity-tests-configuration).

You can also override fuzz and invariant settings for individual test functions using inline configuration. Read the [Inline configuration](/docs/guides/testing/inline-configuration) guide to learn more.
Comment thread
schaable marked this conversation as resolved.
Outdated
2 changes: 1 addition & 1 deletion src/content/docs/docs/guides/testing/using-viem.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Testing your smart contracts with viem and node:test
description: Learn how to test your smart contracts using viem and Node.js test runner.
sidebar:
label: Using TypeScript & viem
order: 3
order: 4
---

import { Steps } from "@astrojs/starlight/components";
Expand Down
Loading