Master Web3 Smart Contract Testing
Web3 Smart Contract Testing covers Hardhat and Foundry unit tests, fuzzing, mainnet forking, and Etherscan verification.
Why it matters
Ensure the robustness and security of your Web3 smart contracts by implementing comprehensive testing strategies. This skill covers unit testing, integration testing, gas optimization, fuzzing, and mainnet forking using industry-standard tools like Hardhat and Foundry.
Outcomes
What it gets done
Set up and configure Hardhat and Foundry for smart contract testing.
Implement unit and integration tests for smart contracts.
Perform gas optimization and fuzzing to identify edge cases and inefficiencies.
Utilize mainnet forking and account impersonation for realistic testing scenarios.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-web3-testing | bash Overview
Web3 Smart Contract Testing
Web3 Smart Contract Testing covers unit, fuzz, and mainnet-fork testing for Solidity contracts across Hardhat and Foundry, plus gas optimization, coverage reporting, and Etherscan verification. Use it for smart contract unit tests, fuzzing, mainnet-fork integration tests, gas optimization, coverage, or Etherscan verification. Not for tasks outside contract testing.
What it does
Web3 Smart Contract Testing covers comprehensive testing strategies for Solidity smart contracts across both Hardhat (JavaScript/TypeScript) and Foundry (Solidity-native, via forge-std/Test.sol) toolchains: unit testing with fixtures, gas-usage assertions, event emission checks, revert-message checks, fuzz testing (Foundry's vm.assume plus fuzzed parameters), mainnet forking to test against real deployed contracts such as Uniswap and DAI, account impersonation and whale testing, snapshot-and-revert state isolation, coverage reporting, and Etherscan contract verification.
When to use - and when NOT to
Use it for writing unit tests for smart contracts, setting up integration test suites, gas optimization testing, fuzzing for edge cases, forking mainnet for realistic testing, automating coverage reporting, or verifying contracts on Etherscan. It is explicitly out of scope for anything unrelated to web3 smart contract testing or when a different domain or tool is what's needed - and, like other skills in this set, defers to resources/implementation-playbook.md for detailed examples rather than dumping everything inline.
Inputs and outputs
Input: a Solidity contract, or a pair of implementations to compare, plus the toolchain in use. Output: a Hardhat config with the solidity-coverage, hardhat-gas-reporter, and hardhat-etherscan plugins wired in, mainnet-forking network config, and a Mocha/Chai test suite using loadFixture for setup, expect(...).to.changeTokenBalances(...) for transfer assertions, .to.emit(...).withArgs(...) for event checks, and time.increase() for time-locked logic - alongside a parallel Foundry test contract using vm.prank, vm.expectRevert, vm.assume for fuzzing, vm.deal, and vm.createSelectFork for its own mainnet-fork tests.
function testFuzzTransfer(uint256 amount) public {
vm.assume(amount > 0 && amount <= token.totalSupply());
vm.prank(owner);
token.transfer(user1, amount);
assertEq(token.balanceOf(user1), amount);
}
It also generates coverage reports (npx hardhat coverage, breaking results down per file into percent statements, branches, functions, and lines covered), verifies contracts on Etherscan (npx hardhat verify --network mainnet ... or the verify:verify Hardhat task), and wires the whole suite into a GitHub Actions CI workflow - compile, test, coverage, Codecov upload - on every push and pull request.
Integrations
Built on Hardhat (with @nomicfoundation/hardhat-toolbox, hardhat-gas-reporter, solidity-coverage, hardhat-etherscan) and Foundry/Forge (forge-std), plus GitHub Actions and Codecov for CI, and live mainnet RPC endpoints for fork testing against real protocols like Uniswap and DAI.
Who it's for
Smart contract developers who need thorough test coverage before deployment - unit tests, fuzz tests, mainnet-fork integration tests, gas benchmarking, and Etherscan verification - across whichever of Hardhat or Foundry the project uses, following the ten named best practices: over 90% coverage, edge-case and reentrancy testing, access-control checks, event verification, fixture reuse, mainnet forking, fuzzing, and CI automation.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.