-
Notifications
You must be signed in to change notification settings - Fork 100
docs: add documentation for inline config for solidity tests #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
94ca93e
docs: add documentation for inline config for solidity tests
schaable 50e1e63
Update src/content/docs/docs/guides/testing/inline-configuration.mdx
schaable c95816e
Update src/content/docs/docs/guides/testing/inline-configuration.mdx
schaable e708b30
Update src/content/docs/docs/guides/testing/using-solidity.mdx
schaable e3afc66
Update src/content/docs/docs/guides/testing/inline-configuration.mdx
schaable f59c82c
apply review changes
schaable 23dfbe8
Linter fixes
alcuadrado File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/content/docs/docs/guides/testing/inline-configuration.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| --- | ||
| title: Inline configuration for Solidity tests | ||
| description: How to use inline comments to override the solidity tests' configuration | ||
| sidebar: | ||
| label: Solidity tests inline config | ||
| order: 5 | ||
| --- | ||
|
|
||
| Hardhat lets you override solidity 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](/docs/reference/configuration#solidity-tests-configuration). | ||
|
|
||
| ## Syntax | ||
|
|
||
| Each override is a single line inside a NatSpec comment (line `///` or block `/** */`), following the format `hardhat-config: <key> = <value>`. | ||
|
|
||
| Keys can be written in camelCase, snake_case, or kebab-case: | ||
|
|
||
| ```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 | ||
|
|
||
| | 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 | | ||
|
|
||
| ### `allowInternalExpectRevert` | ||
|
|
||
| By default, the `expectRevert` cheatcode only catches reverts from external calls, that is, calls at a lower depth than the test itself. If your test directly calls an internal function that reverts, `expectRevert` won't catch it and you'll see an error like `call didn't revert at a lower depth than cheatcode call depth`. | ||
|
|
||
| Setting `allowInternalExpectRevert` to `true` allows `expectRevert` to work on calls at the same depth as the test: | ||
|
|
||
| ```solidity | ||
| /// hardhat-config: allowInternalExpectRevert = true | ||
| function testInternalRevert() public { | ||
| vm.expectRevert("some error"); | ||
| this.myInternalHelper(); // reverts at the same call depth | ||
| } | ||
| ``` | ||
|
|
||
| ## Foundry compatibility | ||
|
|
||
| Hardhat also accepts the `forge-config:` prefix and the `default` profile, so existing Foundry inline configuration works without changes: | ||
|
|
||
| ```solidity | ||
| /// forge-config: default.fuzz.runs = 10000 | ||
| /// forge-config: fuzz.max-test-rejects = 500 | ||
|
schaable marked this conversation as resolved.
|
||
| function testTransferFuzz(uint256 amount) public { | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| Omitting the profile is equivalent to using `default`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.