Measuring test coverage
Hardhat 3 has built-in test coverage functionality to help you identify untested code. It works with Solidity tests by default, and with the hardhat-toolbox-viem plugin, it also works with node:test tests.
Getting code coverage for Solidity tests
Section titled “Getting code coverage for Solidity tests”Generate a coverage report for your Solidity tests:
npx hardhat test solidity --coveragepnpm hardhat test solidity --coverageyarn hardhat test solidity --coverageThe output shows a coverage summary at the end. A Markdown report is printed to the terminal, while LCOV and HTML reports are generated in the coverage folder.
Getting code coverage for TypeScript tests
Section titled “Getting code coverage for TypeScript tests”Generate a coverage report for your TypeScript tests:
npx hardhat test nodejs --coveragepnpm hardhat test nodejs --coverageyarn hardhat test nodejs --coverageGetting combined code coverage
Section titled “Getting combined code coverage”Get combined coverage for both Solidity and TypeScript tests:
npx hardhat test --coveragepnpm hardhat test --coverageyarn hardhat test --coverage