Code-coverage tool for Solidity tests. Instruments contracts, runs your existing Hardhat (or Foundry-via-adapter) test suite, and emits Istanbul-format reports including line, branch, function, and statement coverage.
- 01Hardhat test coverage
- 02CI coverage gates
- 03Istanbul/HTML coverage reports
- 04branch coverage for audits
- 05lcov upload to Codecov/Coveralls
- pnpm add -D solidity-coverage
- pnpm hardhat coverage
Add `import "solidity-coverage";` (or `require('solidity-coverage')`) at the top of `hardhat.config.ts`. Run `npx hardhat coverage` to instrument contracts, execute the test suite, and emit `coverage/` (HTML) plus `coverage.json` and `lcov.info`. Tune via `.solcover.js` — common keys: `skipFiles`, `mocha.timeout`, `configureYulOptimizer`, and `istanbulReporter`. In CI, fail builds below a threshold using `nyc check-coverage --branches 90 --functions 90` against the generated `coverage.json`. For Foundry projects use `forge coverage --report lcov` instead — solidity-coverage is Hardhat-first.
- ⚑Requires Hardhat >= 2.11.0; older Hardhats and the legacy Buidler runner are not supported.
- ⚑Instrumented contracts grow significantly in size and gas — block gas limits, optimizer-related stack-too-deep errors, and timeouts can appear only under coverage. Set `allowUnlimitedContractSize: true` on the test network and bump `mocha.timeout`.
- ⚑Branch coverage of `require`/`if` on `bytes`/`string` comparisons is approximate; treat 100% line coverage as a starting point, not a security guarantee.
- ⚑Coverage and gas-reporter plugins fight over the same hooks — disable `hardhat-gas-reporter` (`REPORT_GAS=false`) when running coverage or numbers will be wrong.
- ⚑Despite the name it does not natively cover Foundry tests — for a Foundry suite use `forge coverage` and translate lcov, or run the same tests under Hardhat.